ElementTree不返回解析简单KML文档的节点

时间:2011-08-23 22:45:27

标签: python

我有一个非常简单的KML文件,在使用ElementTree解析时不返回任何节点。这令我很沮丧:-)。有线索吗?

from   xml.etree  import ElementTree
from   pprint     import pprint

kml = '''<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Document>
  <name>NEXRAD Radar Sites</name>
  <Schema parent="Placemark" name="wsr">
    <SimpleField type="wstring" name="STATE">
    </SimpleField>
  </Schema>
  <wsr>
    <name>KABR</name>
  </wsr>
</Document>
</kml>
'''

tree    = ElementTree.fromstring(kml)
ElementTree.dump(tree)
for node in tree.iter('wsr'):
    pprint(node)
for node in tree.findall('../wsr'):
    pprint(node)

1 个答案:

答案 0 :(得分:4)

标签是命名空间。如果您尝试tree.iter()没有标记,它将显示ElementTree认为标记被调用的内容。 wsr标记名为{http://earth.google.com/kml/2.0}wsr。这将返回一个节点:

list(tree.iter('{http://earth.google.com/kml/2.0}wsr'))