XML: How to get Elements by Attribute Value - Python 2.7 and minidom

minidom, python, python-2.7, xml

Solution

If you don't find a built-in method, why not iterate over the items?

from xml.dom import minidom
xmldoc = minidom.parse(r"C:\File.xml")
PFD = xmldoc.getElementsByTagName("PFD")
PNT = xmldoc.getElementsByTagName("PNT")
for element in PNT:
    if element.getAttribute('A') == "1":
        print "element found"

Adding the items to a list should be easy now.

Problem

I want to get a list of XML Elements based first on TagName and second on Attribute Value. I´m using the xml.dom library and python 2.7. While it´s easy to get the first step done: ``` from xml.dom import minidom xmldoc = minidom.parse(r"C:\File.xml") PFD = xmldoc.getElementsByTagName("PFD") PNT = PFD.getElementsByTagName("PNT") ``` I´ve been looking around but cannot find a solution for the second step. Is there something like a `.getElementsByAttributeValue` that would give me a list to work with? If the XML looks like this ``` <PFD> <PNT A="1" B=.../> <PNT A="1" B=.../> <PNT A="2" B=.../> </PFD> ``` In need all PNTs where A="1" in a list.

Original source