2011-03-01 280 views
0

我正在使用SimpleXML。我想获得这个节点的文本属性。解析XML(PHP)

<yweather:condition text="Mostly Cloudy" ...... 

我使用这个它不工作:

$xml->children("yweather", TRUE)->condition->attributes()->text; 
+0

可能重复的[PHP命名空间simplexml问题](http://stackoverflow.com/questions/2098170/php-namespace-simplexml-problems) – Gordon 2011-03-01 14:29:39

+0

使用DOM时的确切重复:http://stackoverflow.com/questions/ 3268976/how-to-get-the-tag-yweathercondition-from-yahoo-weather-rss-in-php – Gordon 2011-03-01 14:45:22

+0

你的“不工作”代码不工作,因为你做错了:''是你尝试rss/condition时rss/channel/item的孩子。 – Gordon 2011-03-01 15:20:22

回答

0

它看起来像您尝试访问的属性,它存储在阵列中的$ XML-> yweather->属性()这样:

$attributes = $xml->condition->attributes(); 
$weather = $attributes['text']; 

为了应对命名空间,则需要使用children()来获取该名称空间的成员。

$weather_items = $xml->channel->item->children("http://xml.weather.yahoo.com/ns/rss/1.0"); 

这可能有助于提及您所显示的字符串是Feed的一部分,特别是RSS格式的Yahoo Weather Feed。

0

你可能会使用XML的$>的条件,但有可能是之前的分支。

+0

这不工作。另外编辑问题请再看看 – Eray 2011-03-01 14:44:25

1

在$ xml上执行print_r()以查看结构的外观。从那里你应该能够看到如何访问信息。

+0

'SimpleXMLElement Object()'。另外编辑问题请再看一遍 – Eray 2011-03-01 14:44:03