【发布时间】:2016-11-08 08:49:29
【问题描述】:
我想使用 Python xml ElementTree API 解析以下 XML 文件。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foos>
<foo_table>
<!-- bar -->
<fooelem>
<fname>BBBB</fname>
<group>SOMEGROUP</group>
<module>some module</module>
</fooelem>
<fooelem>
<fname>AAAA</fname>
<group>other group</group>
<module>other module</module>
</fooelem>
<!-- bar -->
</foo_table>
</foos>
在此示例代码中,我尝试查找/foos/foo_table/fooelem/fname 下的所有元素,但显然findall 在运行此代码时没有找到任何内容。
import xml.etree.cElementTree as ET
tree = ET.ElementTree(file="min.xml")
for i in tree.findall("./foos/foo_table/fooelem/fname"):
print i
root = tree.getroot()
for i in root.findall("./foos/foo_table/fooelem/fname"):
print i
我没有使用 ElementTree API 的经验,但我使用了 https://docs.python.org/2/library/xml.etree.elementtree.html#example 下的示例。为什么它在我的情况下不起作用?
【问题讨论】:
标签: python xml python-2.7