Over a million developers have joined DZone.

Using ElementTree

·
ElementTree is a library that make XML processing in python
much, much easier. It will be included in Python 2.5 too.
Here's how to use it to read/extract XML

from elementtree.ElementTree import parse
tree = parse(filename)
doc = tree.getroot()

# Element type (name): 
print doc.tag
# Element text: 
print doc.text
# get the child of element type book
book = doc.find('book')

# element's attribute
book.keys()
book.items()
book.get('COLOR')

# first matching element's text
booktext = doc.findtext('book')

Read more here and here.
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}