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

时间:2022-10-06 21:02:17

How to acces geo:lat and geo:long in this feed? Can't see it in the var_dump.

如何在这个Feed中访问geo:lat和geo:long?在var_dump中看不到它。

Basically I know how to use the obj returned by simplexml_load_file, however I can't find the geo tags.

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

$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);
}

EDIT: updated code

编辑:更新的代码

1 个解决方案

#1


0  

I made this simple test:

我做了这个简单的测试:

<?php

$feed = simplexml_load_file('http://feeds.livep2000.nl');

echo "<pre>".print_r($feed,true)."</pre>";


?>

The reason why it doesn't show de geo tags it's because they don't exist in the $feed var.

之所以没有显示de geo标签,是因为它们不存在于$ feed var中。

Sample output fo the code:

代码示例输出:

[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
                    )

Now you can access the Lat and Long values in this way:

现在,您可以通过以下方式访问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>";
}

And the proper output of the script:

并且脚本的正确输出:

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

#1


0  

I made this simple test:

我做了这个简单的测试:

<?php

$feed = simplexml_load_file('http://feeds.livep2000.nl');

echo "<pre>".print_r($feed,true)."</pre>";


?>

The reason why it doesn't show de geo tags it's because they don't exist in the $feed var.

之所以没有显示de geo标签,是因为它们不存在于$ feed var中。

Sample output fo the code:

代码示例输出:

[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
                    )

Now you can access the Lat and Long values in this way:

现在,您可以通过以下方式访问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>";
}

And the proper output of the script:

并且脚本的正确输出:

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