简体   繁体   English

XML:如何按属性值获取元素 - Python 2.7和minidom

[英]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. 我想首先在TagName上获取XML元素列表,然后在属性值上获取第二个 XML元素列表。 I´m using the xml.dom library and python 2.7. 我正在使用xml.dom库和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? 是否有类似.getElementsByAttributeValue东西可以给我一个列表来使用?

If the XML looks like this 如果XML看起来像这样

<PFD>
     <PNT A="1" B=.../>
     <PNT A="1" B=.../>
     <PNT A="2" B=.../>
</PFD>

In need all PNTs where A="1" in a list. 需要所有PNT,其中A =“1”在列表中。

If you aren't limited to using xml.dom.minidom, lxml has better search functionality. 如果您不限于使用xml.dom.minidom,则lxml具有更好的搜索功能。 Note that lxml is not builtin, and will require installing the lxml package and non-Python dependencies. 请注意,lxml不是内置的,需要安装lxml包和非Python依赖项。

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') . Lxml还通过element.xpath('query')支持所有XPath 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. 其他方便的功能包括element.findtext指找到适当的元素,并返回它的文本, element.findelement.findall返回匹配使用XPath的覆盖通用查询的子集的查询的所有元件的第一/列表。

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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