尝试解析 php 中的 xml 时缺少节点
Missing nodes when trying to parse xml in php
我正在使用以下供稿:http://feeds.livep2000.nl/
我尝试用下面的代码行来解析它:
$xml = simplexml_load_string(file_get_contents($url), null, LIBXML_NOCDATA);
它显示了我预期的数据,但缺少 geo:lat
和 geo:long
,如下所示:
XML
<item>
<title>
<![CDATA[
A2 (DIA: ) some title
]]>
</title>
<link>http://monitor.livep2000.nl?SPI=1707082033480217</link>
<pubDate>Sat, 08 Jul 2017 20:33:48 +0200</pubDate>
<description>
<![CDATA[
1420999 MKA Rotterdam-Rijnmond ( Monitorcode )<br/>1420029 MKA Rotterdam-Rijnmond ( Ambulance 17-129 )<br/>
]]>
</description>
<guid>1707082033480217</guid>
<author/>
<geo:lat>51.8431255</geo:lat>
<geo:long>4.3429498</geo:long>
</item>
回应
["item"]=>
array(50) {
[0]=>
object(SimpleXMLElement)#163 (6) {
["title"]=>
string(41) "some title"
["link"]=>
string(48) "http://monitor.livep2000.nl?SPI=1707082036240209"
["pubDate"]=>
string(31) "Sat, 08 Jul 2017 20:36:24 +0200"
["description"]=>
string(44) "description ( Ambulance 09-129 )"
["guid"]=>
string(16) "1707082036240209"
["author"]=>
object(SimpleXMLElement)#213 (0) {
}
}
etc....
知道为什么缺少地理节点吗?
问题是 lat
和 long
是上述 XML 和 simplexml
中 geo
命名空间的一部分,不允许您访问默认情况下。
您需要使用类似的东西:
$ns = $xml->getNamespaces(TRUE);
foreach ($xml->channel[0]->item as $item) {
var_dump($item->children($ns['geo'])->lat);
var_dump($item->children($ns['geo'])->lon);
// do other stuff with $item here
}
我正在使用以下供稿:http://feeds.livep2000.nl/
我尝试用下面的代码行来解析它:
$xml = simplexml_load_string(file_get_contents($url), null, LIBXML_NOCDATA);
它显示了我预期的数据,但缺少 geo:lat
和 geo:long
,如下所示:
XML
<item>
<title>
<![CDATA[
A2 (DIA: ) some title
]]>
</title>
<link>http://monitor.livep2000.nl?SPI=1707082033480217</link>
<pubDate>Sat, 08 Jul 2017 20:33:48 +0200</pubDate>
<description>
<![CDATA[
1420999 MKA Rotterdam-Rijnmond ( Monitorcode )<br/>1420029 MKA Rotterdam-Rijnmond ( Ambulance 17-129 )<br/>
]]>
</description>
<guid>1707082033480217</guid>
<author/>
<geo:lat>51.8431255</geo:lat>
<geo:long>4.3429498</geo:long>
</item>
回应
["item"]=>
array(50) {
[0]=>
object(SimpleXMLElement)#163 (6) {
["title"]=>
string(41) "some title"
["link"]=>
string(48) "http://monitor.livep2000.nl?SPI=1707082036240209"
["pubDate"]=>
string(31) "Sat, 08 Jul 2017 20:36:24 +0200"
["description"]=>
string(44) "description ( Ambulance 09-129 )"
["guid"]=>
string(16) "1707082036240209"
["author"]=>
object(SimpleXMLElement)#213 (0) {
}
}
etc....
知道为什么缺少地理节点吗?
问题是 lat
和 long
是上述 XML 和 simplexml
中 geo
命名空间的一部分,不允许您访问默认情况下。
您需要使用类似的东西:
$ns = $xml->getNamespaces(TRUE);
foreach ($xml->channel[0]->item as $item) {
var_dump($item->children($ns['geo'])->lat);
var_dump($item->children($ns['geo'])->lon);
// do other stuff with $item here
}