如何在Python中读取和解析没有模式的XML?
有没有一种方法可以在不使用模式的情况下读取Python中的XML文档?在我的用例中,有一个类似于以下内容的文件如何在Python中读取和解析没有模式的XML?,python,xml,xml-parsing,Python,Xml,Xml Parsing,有没有一种方法可以在不使用模式的情况下读取Python中的XML文档?在我的用例中,有一个类似于以下内容的文件 <people> <human> <weight>75</weight> <height>174</height> </human> <human> <weight>89</weight> <
<people>
<human>
<weight>75</weight>
<height>174</height>
</human>
<human>
<weight>89</weight>
<height>187</height>
</human>
</people>
75
174
89
187
我需要从中提取一个weight
数组。使用字符串操作可以很容易地完成,但必须有一种更干净的方法来使用XML解析器完成此操作?您可以使用ElementTree(包含在python标准库中)并执行以下操作:
import xml.etree.ElementTree
tree = xml.etree.ElementTree.parse("foo.xml")
myArray = [int(x.text) for x in tree.getroot().findall("human/weight")]