def _constructDOM(self): ''' Constructs the XML DOM document that describes the contents of the Request instance. ''' xd = xml.dom.minidom.Document() root_elem = xd.createElement('CityStateLookupRequest') root_elem.setAttribute('USERID', USPS_USER_ID) # We want to add all our addresses. addr_ids = self.__addresses.keys() addr_ids.sort() for addr_id in addr_ids: zc_elem = xd.createElement('ZipCode') zc_elem.setAttribute('ID', addr_id) createXmlElement(zc_elem, 'Zip5', self.__addresses[addr_id]) pass # Don't forget to append the root element, and we're all set. xd.appendChild(root_elem) return xd
def appendToXml(self, address_id, parent_element): """ Generates an XML DOM node beneath the given parent node, forming the expected request XML used by the USPS webtools API. The API allows the user to verify up to 5 addresses per request, so an ID must be assigned to each address which will be verified. """ xd = parent_element.ownerDocument address_element = xd.createElement("Address") address_element.setAttribute("ID", address_id) createXmlElement(address_element, "FirmName", self.__firmName) createXmlElement(address_element, "Address1", self.__address1) createXmlElement(address_element, "Address2", self.__address2) createXmlElement(address_element, "City", self.__city) createXmlElement(address_element, "State", self.__state) createXmlElement(address_element, "Zip5", self.__zip5) createXmlElement(address_element, "Zip4", self.__zip4) parent_element.appendChild(address_element) return