0

这是我的 XML 文件内容:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/ActivityExtension/v2 http://www.garmin.com/xmlschemas/ActivityExtensionv2.xsd http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd">

  <Activities>
    <Activity Sport="Running">
      <Id>2012-11-17T16:41:52Z</Id>
      <Lap StartTime="2012-11-17T16:41:52Z">
        <TotalTimeSeconds>473.6400000</TotalTimeSeconds>
        <DistanceMeters>1600.0000000</DistanceMeters>
      </Lap>
    </Activity>
  </Activities>
</TrainingCenterDatabase>

我想在 Python 中使用 Elemtenttree 选择所有 Lap 节点:

tree = ET.parse(fileName)
elem = tree.getroot()

for ele in elem.findall('.//Lap'):
    print ele

但是上面的代码不起作用..

有谁知道如何选择 Lap 节点?

4

1 回答 1

2

您需要指定命名空间:

>>> tree.findall('.//{http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2}Lap')
[<Element '{http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2}Lap' at 0x4698f90>]
于 2012-11-20T14:29:21.920 回答