简体   繁体   中英

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

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.

If you aren't limited to using xml.dom.minidom, lxml has better search functionality. Note that lxml is not builtin, and will require installing the lxml package and non-Python dependencies.

Eg:

>>> from lxml import etree
>>> root = etree.parse(r"C:\File.xml")
>>> for e in root.findall('PNT[@A="1"]'):
...     print etree.tostring(e)
<PNT A="1" B="c"/>
<PNT A="1" B="b"/>

Lxml also supports all of XPath via element.xpath('query') . Other convenience functions include element.findtext which finds the appropriate element and returns the text of it, element.find and element.findall which returns the first/list of all elements matching a query using a subset of XPath covering common queries.

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.

Try this:

from xml.dom import minidom

xmldoc = minidom.parse(r"C:\File.xml")
PNT = xmldoc.getElementsByTagName("PNT")

for element in PNT:
    print element.attributes.keys()
    for elem in element.attributes.values():
        print elem.firstChild.data 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM