【问题标题】:Python getchildren() not working for valid XML treePython getchildren() 不适用于有效的 XML 树
【发布时间】:2021-07-09 16:36:36
【问题描述】:

如果我在 XML 文件上运行以下 python(见 Q 底部):

import xml.etree.ElementTree as ET
tree = ET.parse('C:\\temp\\test2.xml')
print(tree.getchildren())

我得到错误:

AttributeError: 'ElementTree' 对象没有属性 'getchildren'

我将 XML 上传到在线验证器,它说 XML 没问题。

【问题讨论】:

    标签: python xml elementtree


    【解决方案1】:

    树本身没有getchildren() 方法。

    print(tree.getroot().getchildren())
    

    请注意,getchildren() 已弃用。见the documentation

    【讨论】:

      【解决方案2】:

      getchildren() 已弃用。

      所以使用list(elem),在你的情况下使用list(tree.getroot())

      【讨论】:

      • 有关弃用的更多信息:bugs.python.org/issue29209getchildren 在 Python 2.7 和 3.2 中已被弃用,并在 Python 3.9 中被删除。
      猜你喜欢
      • 2012-04-26
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-21
      • 2012-07-23
      相关资源
      最近更新 更多