Accessor and tag in XML file using ElementTree

Thanks a lot for your reading. I apologize for asking this as a beginner as I am sure it is a simple answer. Any guidance is greatly appreciated.

I have an xml file that I am parsing with ElementTree

which has elements that look like this:

data.xml:
<?xml version="1.0" encoding="utf-8"?><listings><listing id="26496000" dateFirstListed="2012-10-13" dateLastListed="2013-10-06" market="SALE" propertyType="DETACHED" bedrooms="4" latestAskingPrice="314950"><address key="u935dยท0" udprn="50812465" line1="12 Millcroft" line2="Millhouse Green" town="SHEFFIELD" postcode="S36 9AR" /><description>  SOME TEXT HERE </description></listing>

      

I want to access the tag <description>

and <address key>

.

Using the guide outlined at https://docs.python.org/2/library/xml.etree.elementtree.html I write:

import xml.etree.ElementTree
data = xml.etree.ElementTree.parse('data.xml')
root = data.getroot()

      

and iterate over the children:

for child in root:
    print child.tag, child.attrib
>
listing {'dateLastListed': '2013-10-06', 'dateFirstListed': '2012-10-13', 'propertyType': 'DETACHED', 'latestAskingPrice': '314950', 'bedrooms': '4', 'id': '26496000', 'market': 'SALE'}

      

This only gives me children for the tag <listing>

. How do I change the above expression to access <address key>

and <description>

?

Edit: The following guidelines on this matter Parsing XML with Python Accessors

I've tried writing:

for i in root.findall("listing"):
    description = i.find('description')
    print description.text

    >
    AttributeError: 'NoneType' object has no attribute 'text'

      

+3


source to share


1 answer


You can iterate over the lists one at a time and then get the inner children description

and address

. To access the attributes use the .attrib

attribute
:



import xml.etree.ElementTree as ET


data = ET.parse('data.xml')
root = data.getroot()
for listing in root.findall("listing"):
    address = listing.find('address')
    description = listing.findtext('description')

    print(description, address.attrib.get("key"))

      

+2


source







All Articles