I know this has been answered before. I spent my afternoon reading all suggestions and the SimpleXML documentation of course. But this seems a kind of special case and I really can't make this work.
Just to know the context, I am parsing a GPX file in Drupal after its upload by the hook_file_save.
This is what my NetBeans Debugger says about variables.
I tried to read the 'lat' field from the attribute of the field in all the ways people suggested in previous post. What I always retrieve inside the field (see lat5_1, _2, ...) is another SimpleXMLElement with nothing inside but a CLASSNAME variable valued "SimpleXMLElement".
This is an excerpt from the XML file which is a GPX file.
I really don't know where I am doing wrong. I thought it was because I isolated the variable (SimpleXMLElement behaviour is kinda strange) but it isnt'. Thanks for your help.
I'm adding a piece of code to debug with some note on variables watching:
$xml = simplexml_load_file($file->uri);
$trkseg = $xml->trk->trkseg; // OK!
$trkpt = $trkseg->trkpt; // OK!
$trkpt_c = count($trkpt); // OK:1289 items
$latlon5 = $trkpt[5]->attributes(); // OK includes @attributes lat lon = 59.158234 5.867209
$latlon6 = $trkpt[6]->attributes(); // OK includes @attributes lat lon = 59.158225 5.867027
foreach ($trkseg->trkpt as $a => $b) {
$c = $b->attributes()->lat; // $b OK, $c has just classname.
echo $a, '="', $b, "\"\n";
}
$lat5_1 = $latlon5['lat']; // NO: has just CLASSNAME
$lat5_2 = $latlon5->lat; // NO: has just CLASSNAME
// $lat5_3 = $latlon5->attributes()->{'lat'};
// NOT COMPUTED! (says trying to get property of non object)
$attribute = $latlon5->attributes(); // NOT COMPUTED!
$lat5_4 = $attribute['lat']; // NOT COMPUTED!
$lat5_5 = $attribute['lon']; // NOT COMPUTED!
An XML excerpt for debug
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1/">
<metadata>
<time>2009-04-06T19:08:57Z</time>
</metadata>
<trk>
<name>Imported</name>
<trkseg>
<trkpt lat="59.158021" lon="5.868032">
<ele>0</ele>
<time>2007-05-21T06:00:00Z</time>
</trkpt>
<trkpt lat="59.158028" lon="5.868011">
<ele>0</ele>
<time>2007-05-21T06:01:53Z</time>
</trkpt>
[...]
</trkseg>
</trk>
</gpx>