REALLY simple xml reader

Diez B. Roggisch deets at nospam.web.de
Sun Jan 27 12:50:06 EST 2008


Simon Pickles schrieb:
> Hi
> 
> Can anyone suggest a really simple XML reader for python? I just want to 
> be able to do something like this:
> 
> xmlDoc = xml.open("file.xml")
> element = xmlDoc.GetElement("foo/bar")
> 
> ... to read the value of:
> 
> <foo>
>   <bar>42</bar>
> </foo>

Since python2.5, the ElementTree module is available in the standard 
lib. Before 2.5, you can of course install it.

Your code then would look like this:

import xml.etree.ElementTree  as et


doc = """
<foo>
   <bar>42</bar>
</foo>
"""

root = et.fromstring(doc)

for bar in root.findall("bar"):
     print bar.text



Diez




More information about the Python-list mailing list