import xml.etree.ElementTree as ET
tree = ET.ElementTree()
tree.parse("test.xml")
children = tree.find('item')
for child in children:
print(child.tag,child.attrib)
输出:
key {}
display {}
price {}
value {}
import xml.etree.ElementTree as ET
tree = ET.ElementTree()
tree.parse("test.xml")
children = tree.findall('item')
for child in children:
print(child.tag,child.attrib)
输出:
item {}
item {}
由上可以看出:
findall:遍历第一层
find:遍历第一层的第一 子层
xml文件
<?xml version="1.0" encoding="utf-8"?>
<DOCUMENT content_method="full">
<item>
<key>Key1</key>
<display>
<url>https://www.baidu.com/</url>
<title>hhhhh</title>
</display>
<price>28</price>
<value>100</value>
</item>
<item>
<key>Key1</key>
<display>
<url>https://www.baidu.com/</url>
<title>hhhhh</title>
</display>
<price>28</price>
<value>100</value>
</item>
</DOCUMENT>