1


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.

NetBeans Output

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.

An exert of XML 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>
donnadulcinea
  • 1,854
  • 2
  • 25
  • 37
  • 1
    Rather than screenshots, could you provide a small sample of XML code and a simplified version of your code to access it, as text? That way, we can reproduce the problem and test solutions. – IMSoP Feb 19 '14 at 15:12
  • Thank you for your suggestion. I just added the necessary part of code. – donnadulcinea Feb 20 '14 at 01:41

1 Answers1

1

I pulled and modified this code from SimpleXMLElement::attributes

<?php
$string = <<<XML
<a>
 <foo name="one" game="lonely">1</foo>
</a>
XML;

$xml = simplexml_load_string($string);
$attribute = $xml->foo[0]->attributes();
print $attribute['name'];
?>

You can probably use something like:

$attribute = $latlon5->attributes();
print $attribute['lat'];
print $attribute['lon'];

Update:

<?php
$string = <<<XML
<?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>
XML;

$xml = simplexml_load_string($string);
// Single simplified attribute retrieval. 
foreach($xml->trk->trkseg->trkpt[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}

// Iterative attribute retrieval.

// Iterates over all instances of trkpt
foreach($xml->trk->trkseg->trkpt as $a => $b) {
    // iterates over all trkpt attributes
    foreach($b->attributes() as $a => $b) {
        //Sample lat print
        print '<br />';
        //Sample interative print
        echo $a,'="',$b,"\"\n";
    }
}

?>

This gives the following output

lat="59.158021" lon="5.868032"
lat="59.158021"
lon="5.868032"
lat="59.158028"
lon="5.868011" 
Ron Williams
  • 226
  • 1
  • 7
  • Thank you for your suggestion. Yes I also think that should work, but doesn't compute the variables ($attribute is not there when watching). I begin to think it's some compiler problem? I am running a linux quickstart 3.2.0-58-virtual #88-Ubuntu (it's a DrupalPro distribution) and using Netbeans 7.4 with firefox for debugging. I added some code to my question. – donnadulcinea Feb 20 '14 at 01:44
  • I modified the code and am able to retrieve the trkpt lat and lon appropriately. – Ron Williams Feb 20 '14 at 20:44
  • Dear @Ron Williams after trying your code I still couldn't access the variables by the watcher. I can't echo variables since Drupal is making a post html parse. So I wrote all the variables in a debug file, and there they are. Thanks to your new code I could finally focus and understand there is a problem with the NetBeans debugger. Thanks a lot! – donnadulcinea Feb 21 '14 at 07:21