'Node has one child, but I can't acces its name, content or grandchild [duplicate]
I am very new to simpleXML and parsing in general, I am trying to parse a GPX file which is generated by tracking devises. Here is the first node as an exemple, I have been able to retrieve lat, lon ele and time but I can't manage to access the different extensions.
Here is the code I am using to access this part :
foreach ($fichier_gpx->trk->trkseg as $tracksegment){
echo "Nouveau segment <br>";
foreach($tracksegment->trkpt as $trackpoint){
$point = new PointsGPX($trackpoint->time,$trackpoint->attributes()->lat,$trackpoint->attributes()->lon,$trackpoint->ele); //besoin de mettre attributes() pour récupérer lon et lat
$tab_points->append($point);
if(isset($trackpoint->extensions)){
echo "I get here <br>";
echo("There are ".$trackpoint->extensions->count()." children<br>");
var_dump($trackpoint->extensions);
$extensions_child = $trackpoint->extensions->children();
echo $extensions_child->getName()." contains <br>";
var_dump($extensions_child);
echo("<br><br>");
}
}
}
And what it gives me :
However ! I can somehow get the values but not there node's name using this code :
foreach ($fichier_gpx->trk->trkseg as $tracksegment){
foreach($tracksegment->trkpt as $trackpoint){
$point = new PointsGPX($trackpoint->time,$trackpoint->attributes()->lat,$trackpoint->attributes()->lon,$trackpoint->ele); //besoin de mettre attributes() pour récupérer lon et lat
$tab_points->append($point);
if(isset($trackpoint->extensions)){
echo "Point has extensions :<br>";
echo "values : ".$trackpoint->extensions->asXML()."<br><br>";
// foreach($trackpoint->extension->children() as $enfant){
// }
}
}
}
Thank you infinitely for your help !
Solution 1:[1]
You're displaying the XML on a web page, so the web browser is treating the XML tags as unknown HTML tags and ignoring them. Use htmlentities() to encode them so you'll see the tags.
echo "values : ".htmlentities($trackpoint->extensions->asXML())."<br><br>";
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Barmar |



