简体   繁体   English

如何使用iterparse获取xml代码段

[英]how to use iterparse to get xml snippet

I'm trying to parse a large XML KML file. 我正在尝试解析大型XML KML文件。 Inside the python script I have the following commands: 在python脚本中,我具有以下命令:

import xml.etree.ElementTree as Etree
for event, elem in Etree.iterparse("tracts.kml", events=('start', 'end')):
     if event == 'end' and elem.tag == '{http://www.opengis.net/kml/2.2}MultiGeometry':
          print(elem)

The xml looks like this: xml看起来像这样:

<MultiGeometry>
     <Polygon>
         <Altitude>
         </Altitude>
         <coordinates>
         </coordinates>
     </Polygon>
</MultiGeometry>

What I want is to export the text inside <MultiGeometry></MultiGeometry> to include child tags and text inside each of them. 我想要的是在<MultiGeometry></MultiGeometry>导出文本,以在每个子标签中包含子标签和文本。

Meaning the output is a string that looks like: <Polygon>...</Polygon> in a string format. 意味着输出是一个字符串,看起来像: <Polygon>...</Polygon> ,采用字符串格式。

elem.text only assumes that there is values outside of the childtags. elem.text仅假定子标签之外还有其他值。 I want all of it. 我要全部 How do I get all of it? 我如何得到所有这些?

Thanks. 谢谢。

If you want the complete string of an xml element, you can use ElementTree.tostring() function . 如果需要xml元素的完整字符串,则可以使用ElementTree.tostring()函数。 Please note this returns a byte string, (encoded using the encoding that is passed to the method , default for encoding is 'us-ascii' ) , you will need to decode() the value to get the actual string. 请注意,这将返回一个字节字符串,(使用传递给该方法的编码进行编码,默认编码为'us-ascii' ),您将需要将该值decode()以获取实际的字符串。

Example - 范例-

>>> import xml.etree.ElementTree as ET
>>> r = ET.fromstring('''<MultiGeometry>
...      <Polygon>
...          <Altitude>
...          </Altitude>
...          <coordinates>
...          </coordinates>
...      </Polygon>
... </MultiGeometry>''')
>>> ET.tostring(r)
b'<MultiGeometry>\n     <Polygon>\n         <Altitude>\n         </Altitude>\n         <coordinates>\n         </coordinates>\n     </Polygon>\n</MultiGeometry>'
>>> print(ET.tostring(r).decode())
<MultiGeometry>
     <Polygon>
         <Altitude>
         </Altitude>
         <coordinates>
         </coordinates>
     </Polygon>
</MultiGeometry>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM