2015-03-03 50 views
1

解析我有一个XML文件,并在它的中间我有一个这样的块:XML与XMLtree或minidom命名

... 
<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)?

回答

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