tkrajina/gpxpy

GPX parsing not working with extension

Why-not-now opened this issue · 2 comments

#open file
with open('test.gpx', 'r') as gpx_file:
    gpx = gpxpy.parse(gpx_file)

print(gpx.extensions)
print(gpx.metadata_extensions)

# save to a file
with open('output.gpx', 'w') as f:
    f.write(gpx.to_xml())

#open written file again
with open('output.gpx', 'r') as gpx_file:
    gpx_copy = gpxpy.parse(gpx_file)  # GPXXMLSyntaxException: Error parsing XML: not well-formed (invalid token): line 7, column 12

With the above attached code, when I read a file and save it, I cannot retrieve it again until I remove the extension. Getting either the extensions or the metadata extensions returns a blank list. I cannot seem to access the extension to remedy the problem.

For reference, this is the original GPX file (which can be opened)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="details.xsl"?>
<gpx
 version="1.1"
 creator="GaiaGPS for Android"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.topografix.com/GPX/1/1"
 xmlns:topografix="http://www.topografix.com/GPX/Private/TopoGrafix/0/1"
 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.topografix.com/GPX/Private/TopoGrafix/0/1 http://www.topografix.com/GPX/Private/TopoGrafix/0/1/topografix.xsd">
<trk>
<name><![CDATA[New Track 02/02/2023 01:00:00]]></name>
<desc></desc>
<number>13</number>
<extensions>
<line xmlns="http://www.topografix.com/GPX/gpx_style/0/2">
<color>5E7A8C</color>
</line>
</extensions>
<trkseg>
<trkpt lat="0.0000" lon="0.0000">
<ele>0.00</ele>
<time>2023-02-02T01:00:00Z</time>
</trkpt>
<trkpt lat="1.0000" lon="0.0000">
<ele>0.00</ele>
<time>2023-02-02T01:50:00Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>

And this is the written file

<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:topografix="http://www.topografix.com/GPX/Private/TopoGrafix/0/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.topografix.com/GPX/Private/TopoGrafix/0/1 http://www.topografix.com/GPX/Private/TopoGrafix/0/1/topografix.xsd" version="1.1" creator="GaiaGPS for Android">
  <trk>
    <name>New Track 02/02/2023 01:00:00</name>
    <number>13</number>
    <extensions>
      <http://www.topografix.com/GPX/gpx_style/0/2:line>
        <http://www.topografix.com/GPX/gpx_style/0/2:color>5E7A8C</http://www.topografix.com/GPX/gpx_style/0/2:color>
      </http://www.topografix.com/GPX/gpx_style/0/2:line>
    </extensions>
    <trkseg>
      <trkpt lat="0.0" lon="0.0">
        <ele>0.0</ele>
        <time>2023-02-02T01:00:00Z</time>
      </trkpt>
      <trkpt lat="1.0" lon="0.0">
        <ele>0.0</ele>
        <time>2023-02-02T01:50:00Z</time>
      </trkpt>
    </trkseg>
  </trk>
</gpx>```
ekspla commented

See, malformed xml issue #234

Workaround:

gpx.nsmap['gpx_style'] = 'http://www.topografix.com/GPX/gpx_style/0/2'

So ig it is a duplicate of #234, thank you for the workaround