无法在 xml 对象中找到/访问标记


Can't find/access tags in xml object

如何访问此提要中的geo:latgeo:long?在var_dump看不到它。

基本上我知道如何使用simplexml_load_file返回的obj,但是我找不到地理标签。

$feed = simplexml_load_file('http://feeds.livep2000.nl/');
//var_dump($feed);
foreach ($feed->channel->item as $item) {
  $title       = (string) $item->title;
  $description = (string) $item->description;
  $pubDate = (string) $item->pubDate;
  $geo = (string) $item->geo:lat;
  print_r($item);
}

编辑:更新的代码

我做了这个简单的测试:

<?php
$feed = simplexml_load_file('http://feeds.livep2000.nl');
echo "<pre>".print_r($feed,true)."</pre>";

?>

它不显示 de geo 标签的原因是因为它们在 $feed 变量中不存在。

代码的示例输出:

[item] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [title] => SimpleXMLElement Object
                            (
                            )
                        [link] => http://monitor.livep2000.nl?SPI=1311111908030208
                        [description] => SimpleXMLElement Object
                            (
                            )
                        [pubDate] => Mon, 11 Nov 2013 19:08:03 +0100
                        [guid] => 1311111908030208
                    )
                [1] => SimpleXMLElement Object
                    (
                        [title] => SimpleXMLElement Object
                            (
                            )
                        [link] => http://monitor.livep2000.nl?SPI=1311111908010223
                        [description] => SimpleXMLElement Object
                            (
                            )
                        [pubDate] => Mon, 11 Nov 2013 19:08:01 +0100
                        [guid] => 1311111908010223
                    )

现在,您可以通过以下方式访问 Lat 和 Long 值:

$feed = simplexml_load_file('http://feeds.livep2000.nl');
foreach ($feed->channel->item as $item) {
  $ns_dc = $item->children('http://www.w3.org/2003/01/geo/wgs84_pos#');
  echo "Lat: ".$ns_dc->lat."    |  Long: ".$ns_dc->long."</br>";
}

以及脚本的正确输出:

Lat: 52.5123727 | Long: 4.9528409
Lat: 51.52939 | Long: 5.0269987
Lat: 52.4189359 | Long: 4.8860944
Lat: 52.5146807 | Long: 4.7027031