TroyGrosfield.com TroyGrosfield.com

Headline

Parsing XML with Python using ElementTree

Author
by Troy Grosfield
Date
December 18th, 2010
Category
Developer
Story

There are many ways to parse XML in python. I will show you the basics on how to parse XML using ElementTree.

While ElementTree 1.3 and higher is not needed for this example, it’s nice to have because it has better xpath support than the earlier version.  pip install elementtree if you don’t already have it:

pip install http://svn.effbot.org/public/elementtree-1.3/

Sample XML

Here’s a simple small XML snippet representing a person. The XML contains a persons name and address.

<persons>
  <person id="54321">
    <name>
      <first>Troy</first>
      <middle>J</middle>
      <last>Grosfield</last>
    </name>
    <address>
      <street>123 North Lane</street>
      <state>CO</state>
      <city>Denver</city>
      <zip>12345</zip>
    </address>
  </person>
</persons>

Read XML

Here’s a simple script to read and print the sample XML from above.

from elementtree import ElementTree as et
# Load the xml content from a string
content = et.fromstring(xml_from_above)

# Get the person or use the .findall method to get all
# people if there's more than person
person = content.find("person")
first_name = person.find("name/first")
middle_name = person.find("name/middle")
last_name = person.find("name/last")

# Get the persons address
address = person.find("address")
street = address.find("street")
state =  address.find("state")
city= address.find("city")
zip = address.find("zip")

# Print output
print "id: " + person.attrib.get('id')
print "first name: " + first_name.text
print "middle name: " + middle_name.text
print "last name: " + last_name.text
print "street: " + street.text
print "state: " + state.text
print "city: " + city.text
print "zip: " + zip.text

Output

id: 54321
first name: Troy
middle name: J
last name: Grosfield
street: 123 North Lane
state: CO
city: Denver
zip: 12345

This is a fairly basic XML parsing demo, but should give you insight on how to get element values as well as element attribute values from XML content.

Tags
Comments
5 Comments »

5 Comments

Leave a reply

 
  1. Author
    Rob
    Date
    July 24th, 2012 at 5:55 am
    Comment

    Agree with Killer, too many people get carried away with this, Keep-it-simple for the newbies!
    Thanks Troy.

  2. Author
    Homepage
    Date
    June 25th, 2012 at 6:57 pm
    Comment

    … [Trackback]…

    […] There you will find 83780 more Infos: blog.troygrosfield.com/2010/12/18/parsing-xml-with-python-using-elementtree/ […]…

  3. Author
    Azmath
    Date
    June 19th, 2012 at 1:06 am
    Comment

    Thanks very much for this simple example, I believe this is the best example for a beginner like me.

  4. Author
    Elektrische Zahnbuerste
    Date
    November 21st, 2011 at 2:48 pm
    Comment

    … [Trackback]…

    […] Read More Infos here: blog.troygrosfield.com/2010/12/18/parsing-xml-with-python-using-elementtree/ […]…

  5. Author
    killer
    Date
    February 18th, 2011 at 7:06 am
    Comment

    this is the best and the most simplest xml parser I saw on the net… those on the oreilly website contains lot of codes…

    good job… python = simplicity :)