使用GPXPY解析gpx文件会导致格式错误的无效令牌错误

时间:2015-03-13 14:24:40

标签: python xml gpx

我有一些gpx文件,我想要解析,然后输入GIS格式。我已经下载了gpxpy因为我需要它的一些功能,而不仅仅是想从文件中提取lat和lon。但是当我做一个解析器时

import gpxpy
p = gpxpy.parse(path_to_gpx_file)

它给了我这个:

ERROR:root:not well-formed (invalid token): line 1, column 2
Traceback (most recent call last):
  File "C:\Python26\ArcGIS10.0\lib\site-packages\gpxpy\parser.py", line 196, in parse
    self.xml_parser = XMLParser(self.xml)
  File "C:\Python26\ArcGIS10.0\lib\site-packages\gpxpy\parser.py", line 43, in __init__
    self.dom = mod_minidom.parseString(xml)
  File "C:\Python26\ArcGIS10.0\lib\xml\dom\minidom.py", line 1928, in parseString
    return expatbuilder.parseString(string)
  File "C:\Python26\ArcGIS10.0\lib\xml\dom\expatbuilder.py", line 940, in parseString
    return builder.parseString(string)
  File "C:\Python26\ArcGIS10.0\lib\xml\dom\expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
ExpatError: not well-formed (invalid token): line 1, column 2
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Python26\ArcGIS10.0\lib\site-packages\gpxpy\__init__.py", line 32, in parse
    return parser.parse()
  File "C:\Python26\ArcGIS10.0\lib\site-packages\gpxpy\parser.py", line 219, in parse
    raise mod_gpx.GPXXMLSyntaxException('Error parsing XML: %s' % str(e), e)
GPXXMLSyntaxException: Error parsing XML: not well-formed (invalid token): line 1, column 2

花了一些时间谷歌搜索后,这让我怀疑xml结构中有错误。但是,我无法发现它们。

我已使用http://www.validome.org/xml/validate/来验证文件,但它说它们是有效的。

这就是我的gpx文件的样子。我已经减少了这个只包括3个跟踪点,但它仍然给我与完整(35k行)文件相同的错误。

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1"
     xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
     xmlns:wptx1="http://www.garmin.com/xmlschemas/WaypointExtension/v1"
     xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
     creator="Dakota 20" version="1.1"
     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 http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.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">
    <metadata>
        <link href="http://www.garmin.com">
            <text>Garmin International</text>
        </link>
        <time>2015-03-01T16:59:53Z</time>
    </metadata>
    <trk>
        <name>SKI1</name>
        <extensions>
            <gpxx:TrackExtension></gpxx:TrackExtension>
        </extensions>
        <trkseg>
            <trkpt lat="43.3737357836" lon="130.0217922572">
                <ele>166.26</ele>
                <time>2015-03-01T08:34:40Z</time>
            </trkpt>
            <trkpt lat="43.3737673834" lon="130.0218102783">
                <ele>166.22</ele>
                <time>2015-03-01T08:34:42Z</time>
            </trkpt>
            <trkpt lat="43.3737869971" lon="130.0217925087">
                <ele>166.78</ele>
                <time>2015-03-01T08:35:02Z</time>
            </trkpt>
        </trkseg>
    </trk>
</gpx>

编辑可能应该添加:使用python 2.6,gpxpy 0.9.8,pycharm 3.1.1

2 个答案:

答案 0 :(得分:6)

在任何地方(我认为)都没有真正记录,所以我会在这里发布。而不是让解析器尝试打开文件,首先生成文件对象,然后将其提供给解析器,所以:

import gpxpy
f = open(path_to_gpx_file, 'r')
p = gpxpy.parse(f)

我不知道为什么我之前没有尝试过......

答案 1 :(得分:2)

以下是解析函数的当前文档:

In [4]: import gpxpy

In [5]: gpxpy.parse?
Signature: gpxpy.parse(xml_or_file, parser=None)
Docstring:
Parse xml (string) or file object. This is just an wrapper for
GPXParser.parse() function.

parser may be 'lxml', 'minidom' or None (then it will be automatically
detected, lxml if possible).

xml_or_file must be the xml to parse or a file-object with the XML.
File:      ~/venv/gpxpy-test/lib/python3.4/site-packages/gpxpy/__init__.py
Type:      function

它指定您需要传入xml字符串或文件对象,但没有说明接受文件名路径。