I have many rows in a database that contains XML and I'm trying to write a Python script to count instances of a particular node attribute.
My tree looks like:
<foo>
<bar>
<type foobar="1"/>
<type foobar="2"/>
</bar>
</foo>
How can I access the attributes "1"
and "2"
in the XML using Python?
Best Solution
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:And similar, usually pretty simple, code patterns.