Parsing XML using xml.etree.cElementTree
Posted
by Andre
on Stack Overflow
See other posts from Stack Overflow
or by Andre
Published on 2010-06-10T09:17:38Z
Indexed on
2010/06/10
9:23 UTC
Read the original article
Hit count: 495
python
|celementtree
I have the following XML in a string named 'xml':
<?xml version="1.0" encoding="ISO-8859-1"?>
<Book>
<Page>
<Text>Blah</Text>
</Page>
</Book>
I'm trying to get the value Blah out of it but I'm having trouble with xml.etree.cElementTree. I've tried the find() and findtext() methods but nothing. Eventually I did this:
import xml.etree.cElementTree as ET
...
root = ET.fromstring(xml)
element = root.getchildren()[0].getchildren()[0]
Element now equals the element, which is what I want (for this solution anyway), but how do I get the inner text from it? element.text does not work. Any ideas?
PS: I am using Python 2.5 atm.
As an extra question: what is a better way to parse xml strings in python?
© Stack Overflow or respective owner