简体   繁体   中英

From minidom/getElementsByTagName to lxml/xpath

I'm trying to parse a lot of different xml/gpx files to get lat/lon pairs that are an attribute of the node trkpt. I have a working minidom version, but i want to try and have a similar version using lxml and xpath to check if it is faster.

Here is sample xml:

xml = '''<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" xmlns="http://www.topografix.com/GPX/1/1">
 <metadata>
  <time>2015-12-24T12:00:00Z</time>
 </metadata>
 <trk>
  <name>Track 1</name>
  <trkseg>
   <trkpt lat="42.00080" lon="2.79610">
    <ele>39.5</ele>
    <time>2015-12-24T12:00:00Z</time>
   </trkpt>
   <trkpt lat="42.99930" lon="2.79010">
    <ele>39.5</ele>
    <time>2015-12-24T12:01:00Z</time>
   </trkpt>
  </trkseg>
 </trk>
</gpx>
'''

This is the minidom version:

from xml.dom import minidom
minitree = minidom.parseString(xml)
trkpt = minitree.getElementsByTagName('trkpt')

for elem in trkpt:
    print(elem.attributes['lat'].value + ', ' + elem.attributes['lon'].value)

Output:

42.00080, 2.79610
42.99930, 2.79010

Now trying to replicate the exact same thing I used XMLQuire to learn that the xpath to my desired attributes would be dft:trk/dft:trkseg/dft:trkpt/@lat so i came up with this so far:

lxtree = etree.fromstring(xml)
trkpt = lxtree.xpath('dft:trk/dft:trkseg/dft:trkpt', namespaces={'dft': 'http://www.topografix.com/GPX/1/1'})

for elem in trkpt:
    print(trkpt[@lat] + ', ' + trpkt[@lon])

The output is nothing or rather that my print statement is wrong. But I can't tell because a check with print(type(trkpt), len(trkpt), trkpt) tells me: <class 'list'> 0 [] So the list is empty from the getgo. Can someone help me see the error?

Use elem.get() to get the value of an attribute.

lxtree = etree.fromstring(xml)
trkpt = lxtree.xpath('dft:trk/dft:trkseg/dft:trkpt', namespaces={'dft': 'http://www.topografix.com/GPX/1/1'})

for elem in trkpt:
    print(elem.get("lat") + ', ' + elem.get("lon"))

Result:

42.00080, 2.79610
42.99930, 2.79010

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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