php parse gpx

So you’ve been digging about on the internet looking for a working example of a php script that will parse a .gpx file and you keep banging your head against the same walls – little snippets here and there or hideously complicated class parsers when all you really want is a simple, straightforward working example.

(By the way – what’s happened to Google? The number of sites in the top results that are nothing more than scraped versions of the php manual is shocking!)

As for a simple bit of code that reads a gpx file and sends it to a database – look no further.

I’ll spare you all the usual boring stuff about gpx being an xml file which is yadda, yadda…

Firstly, here’s a sample bit of a gpx file. It’s the first few entries for Scotland’s Munros (mountains of 3,000 feet or higher).

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gpx version="1.1" creator="Phils GPX generator"

<wpt lat="56.870408" lon="-4.19884"><ele>936</ele><name>0936.m</name><cmt><![CDATA[A'Bhuidheanach Bheag]]></cmt><desc><![CDATA[A'Bhuidheanach Bheag]]></desc><sym>Waypoint</sym></wpt>
<wpt lat="57.693783" lon="-5.128737"><ele>997</ele><name>0997aM</name><cmt><![CDATA[A'Chailleach]]></cmt><desc><![CDATA[A'Chailleach]]></desc><sym>Waypoint</sym></wpt>
<wpt lat="57.109584" lon="-4.179322"><ele>930</ele><name>0930am</name><cmt><![CDATA[A'Chailleach]]></cmt><desc><![CDATA[A'Chailleach]]></desc><sym>Waypoint</sym></wpt>
<wpt lat="57.184251" lon="-5.154889"><ele>1120</ele><name>1120cM</name><cmt><![CDATA[A'Chralaig]]></cmt><desc><![CDATA[A'Chralaig]]></desc><sym>Waypoint</sym></wpt>

If you would like your own full version of this gpx file you can get one for free from the excellent, site.

Now what you have been searching for – a working example of a php script that will read a .gpx file and write it to a database.


include ("config-dB.php"); //include page that connects to database

// displays all the file nodes
    trigger_error('Error reading XML file',E_USER_ERROR);
echo 'Displaying contents of GPX file...<br />';
foreach($xml as $munro){

	$attrs = $munro->attributes(); //fetches the *attributes* of a tag, e.g. <body type="small" important="low"> - attributes are 'small' and 'low' - required for lat and long 
	$lat = $attrs['lat'];
	$lon = $attrs['lon'];
	//echo the components of the gpx file
	echo 'lat: ' . $lat . ' long: ' . $lon . ' height: ' . $munro->ele . ' name: ' . $munro->name . ' cmt: ' . $munro->cmt . ' desc: ' . $munro->desc . ' sym: ' . $munro->sym . '<br />';
	$ele = $munro->ele; //elevation (height) tag <ele>
	//**name and comment field are wrong way round in gpx file**
	$cmt = $munro->name;
	$name = mysql_real_escape_string($munro->cmt); //the comment tag <cmt>, escaped because this field data contains apostrophes '
	$desc = mysql_real_escape_string($munro->desc); //the description tag <desc>, escaped because this field data also contains apostrophes '
	$sym = $munro->sym; //the symbol tag <sym>
	$query = ("INSERT INTO `munros` (`id`, `Name`, `Lat`, `Long`, `Height`, `Comment`, `Description`, `Symbol`) VALUES ('$i', '$name', '$lat', '$lon', '$ele', '$cmt', '$desc', '$sym')");
	$insert_sql = mysql_query($query);
	echo mysql_error() . "<br />";	 

Notice that the wpt

<wpt lat="56.870408" lon="-4.19884">

tag in the gpx file has two attributes, latitude and longitude, and that an attributes function must be called within the php script to snag the contents of both.