Python Forum
[gpxpy] "Error parsing XML: not well-formed (invalid token): line 1, column 1"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[gpxpy] "Error parsing XML: not well-formed (invalid token): line 1, column 1"
#1
Hello,

For some reason,the gpxpy library isn't happy parsing the following very basic GPX (XML) file:

<?xml version="1.0" encoding="UTF-8"?>
<gpx 
 xmlns="http://www.topografix.com/GPX/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" 
 creator="BRouter-1.5.5" version="1.1">
  <trk>
    <trkseg>
      <trkpt lat="46.718843188136816" lon="-2.339932657778263">
        <ele>0</ele>
      </trkpt>
    </trkseg>
  </trk>
</gpx>


import gpxpy 
import gpxpy.gpx 

f = open('input.gpx', 'r')
"""
Traceback (most recent call last):
gpxpy.gpx.GPXXMLSyntaxException: Error parsing XML: not well-formed (invalid token): line 1, column 1
"""
gpx = gpxpy.parse(f)
Googles shows that some users had the same issue, but offered no solution. Any idea what it could be?

Thank you.
Reply
#2
Hi :)
Unfortunately I could not reproduce the error. I copied your code and created a gpx file like you did and on my system everything worked perfectly. I used Python3.8 and gpxpy v.1.4.0.
What Python and gpxpy version are you using?
Reply
#3
this package was just released 9 days ago.
If you find issues, you should probably contact the author and let him know
[email protected]
Reply
#4
Python 3.7.0 and gpxpy 0.9.8.

Turns out it's an old version, so I ran "pip install gpxpy --upgrade"… but still no go: Other GPX files in the series show the same problem. I wonder if it could be some unprintable character that's keeping gpxpy from parsing them correcly :-/

BTW, Google didn't return any example of how to read and handle <trk> items from a GPX file. Do you know how it's done ?

f = open('input.gpx', 'r')
gpx = gpxpy.parse(f)

#File could have more than one <trk>
for track in gpx.tracks:
	#How to merge multiple trk's into a single GPX file?

	print(track)
	with open("merged.gpx", 'a') as tempf:
		#TypeError: write() argument must be str, not GPXTrack
		#tempf.write(track)
		tempf.close
exit()
Reply
#5
Found it: They all contain "EF BB BF" at the top, like all UTF-8 files are supposed to be.

Removing those bytes solved the issue.

HTH,
Reply
#6
For those needing to remove the BOM from UTF8 files:

import os
import sys
from glob import glob

for filename in glob("*.GPX"):
	basicname, file_extension = os.path.splitext(filename)
	#print(basicname,file_extension)
	s = open(filename, mode='r', encoding='utf-8-sig').read()
	open(f"{basicname}_NOBOM{file_extension}", mode='w', encoding='utf-8').write(s)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why does newly-formed dict only consist of last row of each year? Mark17 6 779 Nov-17-2023, 05:28 PM
Last Post: Mark17
  error: invalid command 'egg_info' TimTu 0 948 Jul-27-2023, 07:30 AM
Last Post: TimTu
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,517 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,915 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  Invalid format specifier Error Led_Zeppelin 2 7,754 Jul-11-2022, 03:55 PM
Last Post: Led_Zeppelin
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,355 Jun-18-2022, 01:09 PM
Last Post: snippsat
  Refresh token for Wyze SDK duckredbeard 0 1,104 May-16-2022, 04:33 AM
Last Post: duckredbeard
  Need to sign JWT token with JWK key stucoder 1 1,680 Feb-21-2022, 09:04 AM
Last Post: stucoder
  How to do next line output from CSV column? atomxkai 2 2,076 Oct-02-2021, 01:00 AM
Last Post: Pedroski55
  Python requests oauth2 access token herobpv 6 3,884 Sep-27-2021, 06:54 PM
Last Post: herobpv

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020