Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access element and tag in xml file using ElementTree

Many thanks for your reading. I apologise for such a beginner question for what I am sure is a simple answer. Any guidance is much appreciated.

I have an xml file which I am parsing with ElementTree, which has elements which 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 <description> tag and <address key>.

Using the guide set out 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 child elements:

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 the child elements for the <listing> tag. How can I change the above expression to access <address key> and <description>?

Edit: Following guidance from this question Parsing XML with Python - accessing elements

I tried writing:

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

    >
    AttributeError: 'NoneType' object has no attribute 'text'
like image 675
Chuck Avatar asked Nov 01 '25 00:11

Chuck


1 Answers

You can iterate over the listings one by one and then get the inner description and address child elements. To access the attributes, use .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"))
like image 194
alecxe Avatar answered Nov 03 '25 14:11

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!