我建议ElementTree。相同的API还有其他兼容的实现,例如
lxml和
cElementTree在
Python标准库本身中。但是,在这种情况下,他们主要添加的是更高的速度-编程的难易程度取决于
ElementTree定义的API 。
首先
root用
XML构建
XML的
Element实例,例如使用
XML函数,或者通过解析文件,例如:
import xml.etree.ElementTree as ETroot = ET.parse('thefile.xml').getroot()或中显示的许多其他方式中的任何一种
ElementTree。然后执行以下操作:
for type_tag in root.findall('bar/type'): value = type_tag.get('foobar') print(value)和类似的,通常很简单的代码模式。



