[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

REALLY simple xml reader

Simon Pickles

1/27/2008 5:35:00 PM

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://coun....



4 Answers

Diez B. Roggisch

1/27/2008 5:50:00 PM

0

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

Mark Tolonen

1/27/2008 5:55:00 PM

0


"Simon Pickles" <sipickles@hotmail.com> wrote in message
news:mailman.1148.1201455326.896.python-list@python.org...
> 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://coun....
>
>
>

>>> from xml.etree import ElementTree as ET
>>> tree=ET.parse('file.xml')
>>> tree.find('bar').text
'42'
>>>

--Mark

Stefan Behnel

1/29/2008 11:41:00 AM

0

Ricardo Aráoz wrote:
> What about :
>
> doc = """
> <moo>
> <bar>99</bar>
> </moo>
> <foo>
> <bar>42</bar>
> </foo>
> """

That's not an XML document, so what about it?

Stefan

Ricardo Aráoz

1/29/2008 12:33:00 PM

0

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>
"""