拥有一个巨大的.xml文件。未找到my previos question的解决方案决定使用interparse()
来获取节点。
问题:无法提取子节点。
.xml示例
<node id="3828373736" lat="54.6849130" lon="25.2850783" version="1" timestamp="2015-11-10T10:52:34Z" changeset="35211986" uid="1241595" user="Runis">
<tag k="highway" v="traffic_sign"/>
<tag k="traffic_sign" v="LT:552"/>
</node>
<node id="3828373737" lat="54.6848815" lon="25.2851614" version="1" timestamp="2015-11-10T10:52:34Z" changeset="35211986" uid="1241595" user="Runis">
<tag k="highway" v="traffic_sign"/>
<tag k="traffic_sign" v="LT:553"/>
</node>
我的部分代码:
import xml.etree.ElementTree as etree
file=('file.osm')
context=etree.iterparse(file, events=('start', 'end', 'start-ns', 'end-ns'))
for event, elem in context:
if event == "start" and elem.tag == "node":
ID=elem.get('id')
lat=elem.get('lat')
lon=elem.get('lon')
print ID,lat,lon
elem.clear()
答案 0 :(得分:0)
可能是我找到了解决方案。如果您分享如何优化此代码的想法,那就太棒了。
import xml.etree.ElementTree as etree
file=('filel.osm')
context=etree.iterparse(file)
for event, elem in context:
if elem.tag == "node":
ID=elem.get('id')
lat=elem.get('lat')
lon=elem.get('lon')
#print ID,lat,lon
for child in elem:
key=child.get('k')
val=child.get('v')
print ID,lat,lon,key,val
child.clear()
elem.clear()