pythonxml ElementTree findall返回空resu

2024-10-03 11:20:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用pythonxmlementtreeapi解析以下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\u table/foolem/fname下的所有元素,但显然findall在运行此代码时找不到任何内容。在

^{pr2}$

我没有使用elementtreeapi的经验,但是我使用了https://docs.python.org/2/library/xml.etree.elementtree.html#example下的示例。为什么在我的案子里没用?在


Tags: 文件代码示例footablegroupbarxml
3条回答

findall不起作用,但可以:

e = xml.etree.ElementTree.parse(myfile3).getroot()
mylist=list(e.iter('checksum'))
print (len(mylist))

我的列表会有适当的长度。在

{root>使用的是元素。
请改用这个:foo_table/fooelem/fname

foos是你的root,你需要从下面的findall开始

root = tree.getroot()
for i in root.findall("foo_table/fooelem/fname"): 
    print i.text

输出:

^{pr2}$

相关问题 更多 >