我正在尝试通过SimpleXMLElement对对象进行xml响应。但是这个操作会使XML元素消失。清楚地看一下XML和SimpleXMLElement:
<item><pubDate>Wed, 28 Dec 2011 13:04:30 GMT</pubDate><title>M 1.2, Nevada</title><description>December 28, 2011 13:04:30 GMT</description><link>http://earthquake.usgs.gov/earthquakes/recenteqsus/Quakes/nn00361989.php</link><geo:lat>37.4048</geo:lat><geo:long>-117.0953</geo:long><dc:subject>1</dc:subject><dc:subject>pasthour</dc:subject><dc:subject>7.00 km</dc:subject><guid isPermaLink="false">nn00361989</guid></item>
这是SimpleXMLElement返回:
[item] => SimpleXMLElement Object
(
[pubDate] => Wed, 28 Dec 2011 13:04:30 GMT
[title] => M 1.2, Nevada
[description] => December 28, 2011 13:04:30 GMT
[link] => http://earthquake.usgs.gov/earthquakes/recenteqsus/Quakes/nn00361989.php
[guid] => nn00361989
)
如你所见,这里没有geo lat和长信息。我正在尝试使用此代码创建SimpleXMLObject:
$doc = new SimpleXMLElement($response)
我必须使用参数吗?
答案 0 :(得分:2)
您的地理数据在XML中是命名空间,因此您需要告诉simplexml还有使用'geo'命名的数据
$doc = new SimpleXMLElement($response)
$namespaces = $doc->getNamespaces(true);
$docGeoData = $doc->item->children($namespaces['geo']);
请注意,您的xml片段格式不正确,因为没有名称空间声明
答案 1 :(得分:0)
也许你应该尝试simplexml_load_file
$doc = simplexml_load_file($response)