简体   繁体   中英

ElementTree does not find time element

In this XML I am trying to find all time elements. (for example 2020-12-10T19:45:20.370Z)

<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="https://github.com/juanirache/gopro-telemetry">
    <trk>
        <name>GH017545.MP4</name>
        <desc>30 fps - GPS (Lat., Long., Alt., 2D speed, 3D speed) - [deg,deg,m,m/s,m/s]</desc>
        <src>Hero7 Black</src>
        <trkseg>
            <trkpt lat="49.0624274" lon="13.7796753">
              <ele>724.267</ele>
              <time>2020-12-10T19:45:20.370Z</time>
              <fix>2d</fix>
              <hdop>604</hdop>
              <geoidheight>46.94805809874912</geoidheight>
              <cmt>2dSpeed: 0.191; 3dSpeed: 0.17</cmt>
          </trkpt>
          <trkpt lat="49.0624294" lon="13.7796749">
              <ele>723.93</ele>
              <time>2020-12-10T19:45:20.425Z</time>
              <fix>2d</fix>
              <hdop>604</hdop>
              <geoidheight>46.94805809874912</geoidheight>
              <cmt>2dSpeed: 0.218; 3dSpeed: 0.19</cmt>
          </trkpt>
...
...
...

But this is not returning anything. Could you please help?

import xml.etree.ElementTree as ET
tree = ET.parse('sample 1.xml')
root = tree.getroot()

for x in root.findall('.//time'):
    print(x.text)

You are getting entangled with namespaces here. There are a couple of ways to handle it; the simplest would be this, I believe:

times = """<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="https://github.com/juanirache/gopro-telemetry">
    <trk>
        <name>GH017545.MP4</name>
        <desc>30 fps - GPS (Lat., Long., Alt., 2D speed, 3D speed) - [deg,deg,m,m/s,m/s]</desc>
        <src>Hero7 Black</src>
        <trkseg>
           
            <trkpt lat="49.0624274" lon="13.7796753">
              <ele>724.267</ele>
              <time>2020-12-10T19:45:20.370Z</time>
              <fix>2d</fix>
              <hdop>604</hdop>
              <geoidheight>46.94805809874912</geoidheight>
              <cmt>2dSpeed: 0.191; 3dSpeed: 0.17</cmt>
          </trkpt>
          <trkpt lat="49.0624294" lon="13.7796749">
              <ele>723.93</ele>
              <time>2020-12-10T19:45:20.425Z</time>
              <fix>2d</fix>
              <hdop>604</hdop>
              <geoidheight>46.94805809874912</geoidheight>
              <cmt>2dSpeed: 0.218; 3dSpeed: 0.19</cmt>
          </trkpt>
          </trkseg>
     </trk>
</gpx>"""

from lxml import etree
doc = etree.XML(times.encode())
for t in doc.xpath('//*[local-name()="time"]/text()'):
    print(t)

Output:

2020-12-10T19:45:20.370Z
2020-12-10T19:45:20.425Z

You have to use the full qualified tag including the namespace:

for element in root.findall('.//{http://www.topografix.com/GPX/1/1}time'):
    print(element.text)

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