xml.dom.minidom
Peter Hansen
peter at engcorp.com
Tue May 21 08:04:00 EDT 2002
joe user wrote:
>
> <?xml version='1.0'?>
> <mydoc version="1.2">
> <title text="testdocument" />
> <text>
> <data>
> Hello World
> </data>
> </text>
> <footer>
> <data>
> Hello Footer
> </data>
> </footer>
> </mydoc>
>
> What I would like to do is to get the contents of the "<data>"-tag
> within the "<footer>"-tag.
The easiest way (for me at least) is to use XPath:
Python 2.2.1 (#34, Apr 9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
>>> import xml.dom.minidom
>>> d = xml.dom.minidom.parse(open('doc.xml'))
>>> d
<xml.dom.minidom.Document instance at 0x009BA340>
>>> d.normalize()
>>> import xml.xpath
>>> xml.xpath.Evaluate('//footer/data', d)
[<DOM Element: data at 10217408>]
>>> xml.xpath.Evaluate('//footer/data', d)[0].firstChild.data
u'\n Hello Footer\n '
Alternatively you could muck with a lot of procedural code with DOM,
or some ugly SAX stuff...
-Peter
More information about the Python-list
mailing list