I have many rows in XML and I'm trying to get instances of a particular node attribute.
<foo>
<bar>
<type foobar="1"/>
<type foobar="2"/>
</bar>
</foo>
How do I access the values of the attribute foobar
? In this example, I want "1"
and "2"
.
Best Answer
I suggest
ElementTree
. There are other compatible implementations of the same API, such aslxml
, andcElementTree
in the Python standard library itself; but, in this context, what they chiefly add is even more speed -- the ease of programming part depends on the API, whichElementTree
defines.First build an Element instance
root
from the XML, e.g. with the XML function, or by parsing a file with something like:Or any of the many other ways shown at
ElementTree
. Then do something like:Output: