REALLY simple xml reader

S

Simon Pickles

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>


Thanks

Simon
 
D

Diez B. Roggisch

Simon said:
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
 
M

Mark Tolonen

Simon Pickles said:
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>


Thanks

Simon

--
Linux user #458601 - http://counter.li.org.

--Mark
 
R

Ricardo Aráoz

Diez said:
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>
"""
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top