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

时间:2022-12-25 22:38:52

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.

我想首先在TagName上获取XML元素列表,然后在属性值上获取第二个XML元素列表。我使用的是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”在列表中。

3 个解决方案

#1


3  

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.

将项目添加到列表应该很容易。

#2


6  

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.

如果您不限于使用xml.dom.minidom,则lxml具有更好的搜索功能。请注意,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'). 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.

Lxml还通过element.xpath('query')支持所有XPath。其他便利函数包括找到适当元素并返回其文本的element.findtext,element.find和element.findall,它使用覆盖常见查询的XPath子集返回与查询匹配的所有元素的第一个/列表。

#3


0  

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 

#1


3  

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.

将项目添加到列表应该很容易。

#2


6  

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.

如果您不限于使用xml.dom.minidom,则lxml具有更好的搜索功能。请注意,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'). 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.

Lxml还通过element.xpath('query')支持所有XPath。其他便利函数包括找到适当元素并返回其文本的element.findtext,element.find和element.findall,它使用覆盖常见查询的XPath子集返回与查询匹配的所有元素的第一个/列表。

#3


0  

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