使用interparse()在和/ 节点之间获取子节点

时间:2022-09-26 11:15:01

Have a huge .xml file. Didn't find solution to my previos question Decided to use interparse() to get node. Problem: can't extract children nodes.

有一个巨大的.xml文件。没有找到解决我的previos问题的决定决定使用interparse()来获取节点。问题:无法提取子节点。

.xml sample

<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>

Part of my code:

我的部分代码:

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()

1 个解决方案

#1


0  

Might be that I found the solution. It would be great if you share your ideas how to optimize this code.

可能是我找到了解决方案。如果您分享如何优化此代码的想法,那就太棒了。

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()

#1


0  

Might be that I found the solution. It would be great if you share your ideas how to optimize this code.

可能是我找到了解决方案。如果您分享如何优化此代码的想法,那就太棒了。

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()