我有一个xml文件,在它的中间我有一个这样的块:
...
<node id = "1" >
<ngh id = "2" > 100 </ngh>
<ngh id = "3"> 300 </ngh>
</node>
<node id = "2">
<ngh id = "1" > 400 </ngh>
<ngh id = "3"> 500 </ngh>
</node>
...
并尝试获取
1, 2, 100
1, 3, 300
2, 1, 400
2, 3, 500
...
我发现了一个类似的问题,并做了以下
from xml.dom import minidom
xmldoc = minidom.parse('file.xml')
nodelist = xmldoc.getElementsByTagName('node')
for s in nodelist:
print s.attributes['id'].value)
有没有办法让我得到标签之间的值(即100,300,400)?
答案 0 :(得分:3)
您需要一个超过ngh
元素的内循环:
from xml.dom import minidom
xmldoc = minidom.parse('file.xml')
nodes = xmldoc.getElementsByTagName('node')
for node in nodes:
node_id = node.attributes['id'].value
for ngh in node.getElementsByTagName('ngh'):
ngh_id = ngh.attributes['id'].value
ngh_text = ngh.firstChild.nodeValue
print node_id, ngh_id, ngh_text
打印:
1 2 100
1 3 300
2 1 400
2 3 500