ElementTree 找不到时间元素

ElementTree does not find time element

在这个 XML 中,我试图找到所有时间元素。 (例如 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>
...
...
...

但这并没有返回任何东西。你能帮忙吗?

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

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

您在这里与命名空间纠缠不清。有几种方法可以处理它;最简单的就是这个,我相信:

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)

输出:

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

您必须使用包括命名空间在内的完全限定标签:

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