遍历元素嵌套结构中的所有XML节点

时间:2012-10-01 10:26:57

标签: python xml python-2.7 minidom

我有这种XML结构(从JSON转换的Esprima ASL输出),它可以比这更嵌套(ASL.xml):

<?xml version="1.0" encoding="UTF-8" ?>
    <program>
    <type>Program</type>
    <body>
        <type>VariableDeclaration</type>
        <declarations>
            <type>VariableDeclarator</type>
            <id>
                <type>Identifier</type>
                <name>answer</name>
            </id>
            <init>
                <type>BinaryExpression</type>
                <operator>*</operator>
                <left>
                    <type>Literal</type>
                    <value>6</value>
                </left>
                <right>
                    <type>Literal</type>
                    <value>7</value>
                </right>
            </init>
        </declarations>
        <kind>var</kind>
    </body>
    </program> 

Usualy for XML我使用for node in root.childNodes`但这仅适用于直接子项:

import xml.dom.minidom as  md
dom = md.parse("ASL.xml")
root = dom.documentElement
for node in root.childNodes:
    if node.nodeType == node.ELEMENT_NODE:
        print node.tagName,"has value:",  node.nodeValue:, "and is child of:",node.parentNode.tagName 

无论有多少嵌套元素,我如何遍历XML的所有元素?

3 个答案:

答案 0 :(得分:9)

这可能是使用递归函数最好的方法。像这样的东西应该这样做,但我没有测试它,所以考虑它伪代码。

import xml.dom.minidom as  md

def print_node(root):
    if root.childNodes:
        for node in root.childNodes:
           if node.nodeType == node.ELEMENT_NODE:
               print node.tagName,"has value:",  node.nodeValue, "and is child of:", node.parentNode.tagName
               print_node(node)

dom = md.parse("ASL.xml")
root = dom.documentElement
print_node(root)

答案 1 :(得分:2)

如果使用xml.dom.minidom不重要:

import xml.etree.ElementTree as ET
tree = ET.fromstring("""...""")
for  elt in tree.iter():
    print "%s: '%s'" % (elt.tag, elt.text.strip())

输出:

program: ''
type: 'Program'
body: ''
type: 'VariableDeclaration'
declarations: ''
type: 'VariableDeclarator'
id: ''
type: 'Identifier'
name: 'answer'
init: ''
type: 'BinaryExpression'
operator: '*'
left: ''
type: 'Literal'
value: '6'
right: ''
type: 'Literal'
value: '7'
kind: 'var'

答案 2 :(得分:1)

对于2.6+等效的kalgasnik的Elementree代码,只需用getiterator()替换iter():

import xml.etree.ElementTree as ET
tree = ET.fromstring("""...""")
for  elt in tree.getiterator():
    print "%s: '%s'" % (elt.tag, elt.text.strip())