Python minidom:如何获取具有相同属性值的第一个子节点的父节点

时间:2014-01-01 19:10:22

标签: python xml minidom

我有一个巨大的xml文件。我在这里只给出一个样本:

<?xml version="1.0"?>
<data yahoo="state">
<country name="Cambodia">
    <neighbor name="Thailand" direction="W"/>
    <neighbor name="Vietnam" towards="E"/>
</country>
<country name="Singapore">
    <neighbor name="Malaysia" dimension="N"/>
</country>
<country name="Panama">
    <neighbor name="Costa Rica" dimension="W"/>
    <neighbor name="Colombia" towards="E"/>
</country>
</data>

我必须只获得其子节点(邻居)的属性(方向)和(维度)具有相同值(W)的父节点(国家/地区)。输出应如下所示:

[<country name="Cambodia">, <country name="Panama">]
正如我们在输出中看到的那样,我们有那些具有相同属性值的子节点的父节点列表,在这种情况下(direction =“W”)和(dimension =“W”)具有相同的值。 我是一个绝对的初学者。我正在尝试这个(这不正确,但只是为了你理解我的尝试):

from xml.dom import minidom
xmldoc = minidom.parse("C:/Users/Dsduda/Desktop/Countries.xml")
x = xmldoc.getElementsByTagName("neighbor")
y = xmldoc.getElementsByTagName("neighbor")
for s in x and t in y:
    if s.attributes["direction"].value == t.attributes["dimension"].value:
          print s.parentNode, t.parentNode

1 个答案:

答案 0 :(得分:1)

将您的邻居节点映射到字典中,按照direction值对其进行分组:

from collections import defaultdict

by_direction = defaultdict(list)

# map directions
for neighbor in xmldoc.getElementsByTagName("neighbor"):
    if not 'direction' in neighbor.attributes:
        continue
    direction = neighor.attributes['direction'].value
    by_direction[direction].append(neighbor)

# look up by dimension:
for neighbor in xmldoc.getElementsByTagName("neighbor"):
    if not 'dimension' in neighbor.attributes:
        continue
    dimension = neighor.attributes['dimension'].value
    print neighbor.parentNode
    for node in by_direction[dimension]:
        print node.parentNode
相关问题