Python getchildren() 不适用于有效的 XML 树

Python getchildren() not working for valid XML tree

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

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

我收到错误:

AttributeError: 'ElementTree' object has no attribute 'getchildren'

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

树本身没有getchildren()方法。

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

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

getchildren() 已弃用。

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