REALLY simple xml reader

Ricardo Aráoz ricaraoz at gmail.com
Tue Jan 29 07:33:14 EST 2008


Diez B. Roggisch wrote:
> 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
> 

What about :

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





More information about the Python-list mailing list