读取SimpleXML元素属性不起作用

时间:2014-02-19 12:10:06

标签: xml drupal netbeans simplexml

我知道以前已经回答了这个问题。我花了我的下午阅读所有建议和当然的SimpleXML文档。但这似乎是一种特殊情况,我真的无法做到这一点。

只是为了解上下文,我在使用hook_file_save上传后在Drupal中解析GPX文件。

这是我的NetBeans调试器关于变量的说法。

NetBeans Output

我尝试按照人们在之前帖子中建议的所有方式从字段的属性中读取'lat'字段。我总是在字段内检索的内容(参见lat5_1,_2,...)是另一个 SimpleXMLElement ,内部没有任何内容,但 CLASSNAME 变量值“ SimpleXMLElement

这是XML文件的摘录,它是一个GPX文件。

An exert of XML File

我真的不知道我做错了什么。我以为是因为我隔离了变量(SimpleXMLElement行为有点奇怪)但它不是'。 谢谢你的帮助。

我正在添加一段代码来调试,并注意有关变量的注意事项:

$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!

调试的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>

1 个答案:

答案 0 :(得分:1)

我从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'];
?>

你可以使用类似的东西:

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

更新

<?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";
    }
}

?>

这给出了以下输出

lat="59.158021" lon="5.868032"
lat="59.158021"
lon="5.868032"
lat="59.158028"
lon="5.868011"