/** * @param SimpleXMLElement $xml * @return GPX * @throws InvalidGPXException */ public static function fromXML(SimpleXMLElement $xml) { if ($xml->getName() != 'gpx') { throw new InvalidGPXException("Root node should be 'gpx'"); } $attributes = $xml->attributes(); $version = $attributes['version']; if ($version != 1.1) { throw new InvalidGPXException("Invalid GPX version. This library only supports GPX 1.1"); } if (!($creator = $attributes['creator'])) { throw new InvalidGPXException("No creator specified on GPX node."); } $gpx = new GPX((double) $version, (string) $creator); if (!empty($xml->metadata)) { $gpx->setMetadata(Metadata::fromXML($xml->metadata[0])); } if (!empty($xml->wpt)) { $waypoints = []; foreach ($xml->wpt as $waypoint) { array_push($waypoints, Waypoint::fromXML($waypoint)); } $gpx->setWaypoints($waypoints); } if (!empty($xml->rte)) { $routes = []; foreach ($xml->rte as $route) { array_push($routes, Route::fromXML($route)); } $gpx->setRoutes($routes); } if (!empty($xml->trk)) { $tracks = []; foreach ($xml->trk as $track) { array_push($tracks, Track::fromXML($track)); } $gpx->setTracks($tracks); } if (!empty($xml->extensions)) { $gpx->setExtensions(Extensions::fromXML($xml->extensions[0])); } return $gpx; }