使用Python ElementTree.register_namespace读取GPX吗? [英] Read GPX using Python ElementTree.register_namespace?
问题描述
我已经将头撞墙了一段时间了。根据文档,这应该很简单。我要做的就是读取GPX文件。但是,GPX文件自由使用名称空间,这从理论上讲是合理的。我似乎无法让ElementTree读取它们。这是我要使用的代码...
I have been beating my head against the wall for some time now. According to the documentation, this should be simple. All I want to do is read a GPX file. However, GPX files liberally use namespaces, which theoretically make sense. I cannot seem to get ElementTree to read them, though. Here is the code I am trying to use...
def loadGpx(self, sourceFile):
ElementTree.register_namespace('gpx', 'http://www.topografix.com/GPX/1/1')
eTree = ElementTree.ElementTree()
eTree.parse(sourceFile)
print eTree.findall('wpt')
从GPX文件中提取航路点,例如此...
To pull out waypoints from a GPX file like this...
<?xml version="1.0" encoding="utf-8"?>
<gpx creator="Garmin Desktop App" version="1.1"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd
http://www.garmin.com/xmlschemas/WaypointExtension/v1
http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd
http://www.garmin.com/xmlschemas/TrackPointExtension/v1
http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd
http://www.garmin.com/xmlschemas/GpxExtensions/v3
http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd
http://www.garmin.com/xmlschemas/ActivityExtension/v1
http://www8.garmin.com/xmlschemas/ActivityExtensionv1.xsd
http://www.garmin.com/xmlschemas/AdventuresExtensions/v1
http://www8.garmin.com/xmlschemas/AdventuresExtensionv1.xsd"
xmlns="http://www.topografix.com/GPX/1/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wptx1="http://www.garmin.com/xmlschemas/WaypointExtension/v1"
xmlns:gpxtrx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
xmlns:abp="http://www.garmin.com/xmlschemas/ActivityExtension/v1"
xmlns:adv="http://www.garmin.com/xmlschemas/AdventuresExtensions/v1">
<metadata>
<link href="http://www.garmin.com">
<text>Garmin International</text>
</link>
<time>2012-01-17T03:21:12Z</time>
<bounds maxlat="45.708811283111572" maxlon="-121.3884991966188"
minlat="45.407062936574221" minlon="-121.54939779080451" />
</metadata>
<wpt lat="45.708682453259826" lon="-121.51224257424474">
<time>2012-01-06T19:00:02Z</time>
<name>1-State and First, start MHL</name>
<sym>Bike Trail</sym>
<extensions>
<gpxx:WaypointExtension>
<gpxx:DisplayMode>SymbolAndName</gpxx:DisplayMode>
</gpxx:WaypointExtension>
</extensions>
</wpt>
<wpt lat="45.615267734974623" lon="-121.43857721239328">
<time>2012-01-07T15:38:14Z</time>
<name>10-Right at fork staying on Huskey Rd</name>
<sym>Bike Trail</sym>
<extensions>
<gpxx:WaypointExtension>
<gpxx:DisplayMode>SymbolAndName</gpxx:DisplayMode>
</gpxx:WaypointExtension>
</extensions>
</wpt>
是的,不仅仅需要 print eTree.findall('wpt ')
,但是如果我能做到的话,我以前就使用过xml。这部分很容易。
True, it will take more than just print eTree.findall('wpt')
, but if I can get that far, I have worked with xml before. That part is easy. This namespace thing though, is driving me nuts.
在此先感谢您。
推荐答案
register_namespace()
控制序列化XML时使用的前缀,
register_namespace()
controls the prefixes used when serializing XML, but it does not affect parsing.
使用ElementTree,请执行以下操作:
With ElementTree, do it like this:
from xml.etree import ElementTree as ET
tree = ET.parse("gpx.xml")
for elem in tree.findall("{http://www.topografix.com/GPX/1/1}wpt"):
print elem
结果输出:
<Element '{http://www.topografix.com/GPX/1/1}wpt' at 0x201c550>
<Element '{http://www.topografix.com/GPX/1/1}wpt' at 0x201c730>
使用 lxml ,您还可以使用以下方法:
With lxml, you can also use this:
from lxml import etree
NSMAP = {"gpx": "http://www.topografix.com/GPX/1/1"}
tree = etree.parse("gpx.xml")
for elem in tree.findall("gpx:wpt", namespaces=NSMAP):
print elem
这篇关于使用Python ElementTree.register_namespace读取GPX吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!