I'm parsing an xml file and using xml.etree.ElementTree module. Excerpt of the file I'm using is below.
I have the absolute path for ZIP Code, and many others. The path for ZIP Code is: "Return/ReturnHeader/Filer/USAddress/ZIPCd". I'm trying to get the ZIP Code out of this xml, but couldn't get that. (I'm preferring this way of writing the path, as there are duplicates and it makes easier for me to get all other values as well).
Sample XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Return xmlns="http://www.irs.gov/efile" xsi:schemaLocation="http://www.irs.gov/efile" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" returnVersion="2014v5.0">
<ReturnHeader binaryAttachmentCnt="0">
<ReturnTs>2016-11-14T11:59:09-06:00</ReturnTs>
<Filer>
<EIN>942788907</EIN>
<PhoneNum>9162866665</PhoneNum>
<USAddress>
<CityNm>SACRAMENTO</CityNm>
<StateAbbreviationCd>CA</StateAbbreviationCd>
<ZIPCd>95833</ZIPCd>
</USAddress>
</Filer>
</ReturnHeader>
</Return>
Any help is appreciated.
import xml.etree.ElementTree as ET
tree = ET.parse('201533089349301428.xml')
root = tree.getroot()
zipn = root.find("Return/ReturnHeader/Filer/USAddress/ZIPCd")
zip = zipn.text
print(zip)