【问题标题】:Walk through all XML nodes in an element-nested structure遍历元素嵌套结构中的所有 XML 节点
【发布时间】:2012-10-01 10:26:56
【问题描述】:

我有这种 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> 

对于 XML,我通常使用 for node inroot.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 的所有元素?

【问题讨论】:

    标签: python xml python-2.7 minidom


    【解决方案1】:

    这可能最好通过递归函数来实现。像这样的东西应该可以,但我没有测试过,所以认为它是伪代码。

    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)
    

    【讨论】:

    • 正是我所追求的那种东西。任何人都可以详细说明以下代码以列出 xml 文件中的属性。谢谢。
    【解决方案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'
    

    【讨论】:

    • 知道如何修改此代码以显示其子元素或其他属性。谢谢。
    【解决方案3】:

    对于相当于 kalgasnik 的 Elementree 代码的 2.6+ 版本,只需将 iter() 替换为 getiterator():

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

    【讨论】:

      猜你喜欢
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      • 2013-07-20
      • 2021-12-24
      相关资源
      最近更新 更多