Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中读取和解析没有模式的XML?_Python_Xml_Xml Parsing - Fatal编程技术网

如何在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> <

有没有一种方法可以在不使用模式的情况下读取Python中的XML文档?在我的用例中,有一个类似于以下内容的文件

<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")]