0

I'm using SimpleXML . I want to get this node's text attribute.

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

I'm using this it's not working :

$xml->children("yweather", TRUE)->condition->attributes()->text;
Eray
  • 7,038
  • 16
  • 70
  • 120
  • possible duplicate of [PHP namespace simplexml problems](http://stackoverflow.com/questions/2098170/php-namespace-simplexml-problems) – Gordon Mar 01 '11 at 14:29
  • Exact duplicate when using DOM: http://stackoverflow.com/questions/3268976/how-to-get-the-tag-yweathercondition-from-yahoo-weather-rss-in-php – Gordon Mar 01 '11 at 14:45
  • your "is not working" code is not working because you are doing it wrong: `` is a child of `rss/channel/item` while you are trying `rss/condition` – Gordon Mar 01 '11 at 15:20

3 Answers3

1

Do a print_r() on $xml to see how the structure looks. From there you should be able to see how to access the information.

Josh Pennington
  • 6,418
  • 13
  • 60
  • 93
0

You would probably use $xml->condition but there may be branches before that.

MrPHP
  • 152
  • 3
  • 12
0

It looks like you are trying to access an attribute, which is stored in an array in $xml->yweather->attributes() so:

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

To deal with the namespace, you need to use children() to get the members of that namespace.

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

It might help to mention that the string you showed is part of a feed, specifically the RSS formatted Yahoo Weather feed.

Wige
  • 3,788
  • 8
  • 37
  • 58