I've been trying to use the lxml package's "objectify" to parse my XMLs and I've come across a problem. If I have a valueless tag, I can't seem to find a way to get its attributes.
For instance:
import lxml.objectify
xml_obj = lxml.objectify.fromstring("""
<A>
<B foo="baz"/>
<B foo="bar"/>
</A>""")
print xml_obj.getchildren()
A = None [ObjectifiedElement]
B = u'' [StringElement]
* baz = 'boo'
B = u'' [StringElement]
* foo = 'bar'
As you can see, the two B tags are turned into StringElement, but as seen when dumping the object, there should still be a way to retrieve the attributes!
You can access the element's attributes using
elt.attrib
:You can modify those attributes as well:
Serializing
xml_obj
withET.tostring
shows the result: