Python xml.dom, help reading attribute data
Alan Kennedy
alanmk at hotmail.com
Tue Sep 6 11:09:31 EDT 2005
[Thierry Lam]
> Let's say I have the following xml tag:
>
> <para role="success">1</para>
>
> I can't figure out what kind of python xml.dom codes I should invoke to
> read the data 1? Any help please?
This is job for xpath.
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
from xml import xpath
from xml.dom import minidom
doc = """<para role="success">1</para>"""
mydom = minidom.parseString(doc)
#result_nodes = xpath.Evaluate("/para/text()", mydom)
#result_nodes = xpath.Evaluate("/para[1]/text()", mydom)
result_nodes = xpath.Evaluate("/para[@role='success']/text()", mydom)
for ix, r in enumerate(result_nodes):
print "result %d: %s" % (ix, r)
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Xpath support is a part of the PyXML package, which you can get from here
http://pyxml.sourceforge.net
Xpath tutorials from here
http://www.zvon.org/xxl/XPathTutorial/General/examples.html
http://www.w3schools.com/xpath/
there-are-other-ways-to-do-it-but-i-like-xpath-ly'yrs,
--
alan kennedy
------------------------------------------------------
email alan: http://xhaus.com/contact/alan
More information about the Python-list
mailing list