I was using MINIDOM but it does not provide xpath methods.
I am now trying to use libxml2 but I am having trouble retrieving attribute values.
An extract of my xml looks as follow:
<Class name="myclass1" version="0">
<Owner user-login="smagnoni"/>
</Class>
and I wrote the following code:
import libxml2
doc = libxml2.parseFile(file)
ris = doc.xpathEval('*/Class[@name="'+className+'" and @version="'+classVersion+'"]/Owner')
print str(ris[0])
which returns:
<Owner user-login="smagnoni"/>
How do I get just "smagnoni"? Parsing the string by hand feels overworked. but I did not find a method comparable to .getAttribute("attribute-name")
in minidom.
Can anyone suggest the proper method or direct me to documentation?
lxml uses libxml2 and provides a nicer interface (the ElementTree api) so you get most of the benefit of libxml2's speed and all of the benefit of it's xpath evaluation.
The added bonus is that the Element Tree api is available by default in python2.5 (though the version in 1.5 does not include the
[@name='value']
xpath syntax, that was added in python 2.7, but you can get the 1.3 api as a separate package in older 2.x versions of python).You can import any compatible version of the ElementTree api using:
.prop('user-login')
should work:yields