View Full Version : Accessing XML element

03-01-2010, 08:21 AM
Hello, Yahoo Weather has following XML structure (showing part of it):

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<title>Yahoo! Weather - Sunnyvale, CA</title>
<description>Yahoo! Weather for Sunnyvale, CA</description>
<lastBuildDate>Fri, 18 Dec 2009 9:38 am PST</lastBuildDate>
<yweather:location city="Sunnyvale" region="CA" country="United States"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="50" direction="0" speed="0" />
<yweather:atmosphere humidity="94" visibility="3" pressure="30.27" rising="1" />
<yweather:astronomy sunrise="7:17 am" sunset="4:52 pm"/>
<title>Yahoo! Weather</title>
<title>Conditions for Sunnyvale, CA at 9:38 am PST</title>
<pubDate>Fri, 18 Dec 2009 9:38 am PST</pubDate>
<yweather:condition text="Mostly Cloudy" code="28" temp="50" date="Fri, 18 Dec 2009 9:38 am PST" />
<img src="http://l.yimg.com/a/i/us/we/52/28.gif"/><br />
<b>Current Conditions:</b><br />
Mostly Cloudy, 50 F<BR />
<BR /><b>Forecast:</b><BR />
Fri - Partly Cloudy. High: 62 Low: 49<br />
Sat - Partly Cloudy. High: 65 Low: 49<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
<yweather:forecast day="Fri" date="18 Dec 2009" low="49" high="62" text="Partly Cloudy" code="30" />
<yweather:forecast day="Sat" date="19 Dec 2009" low="49" high="65" text="Partly Cloudy" code="30" />
<guid isPermaLink="false">USCA1116_2009_12_18_9_38_PST</guid>

Accessing regular xml elements with PHP is no problem:

$title = $xml->channel->item->title;
$description = $xml->channel->item->description;

However, I get parse errors when accessing elements in form of <yweather:location>...

$condition = $xml->channel->item->yweather:condition[text];

gives me a parse error...

What is the correct PHP syntax to access the attributes?

Thanks for your help,


03-01-2010, 09:02 AM
Hi guvenck,

Good day!

The actual tag name is location. The "yweather:" is the namespace, so try accessing them without it.

Hope that helps.

03-01-2010, 11:44 AM
not sure this will work. you have to access the elements with their namespace (ref. (http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/)). more on google.

03-01-2010, 11:58 AM

Thank you for your answer,

I tried these and both of them did not work:

// this throws an empty value
$condition = $xml->channel->item->condition[text];

// this throws an error:
// Warning: main() [function.main]: Node no longer exists in weather.php on line 14

$condition = $xml->channel->item->condition->attributes()->text;

Any more ideas?

03-01-2010, 12:00 PM
Any more ideas?

read the article in the given link, that should explain it.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum