Accessing namespaces xml/rss

3 messages Options
Embed this post
Permalink
kwylez

Accessing namespaces xml/rss

Reply Threaded More More options
Print post
Permalink
I am trying to parse the Yahoo! rss weather feed, specifically their values inside of namespaces, but I can't find the right path to save my life. I have registered the namespaces, but can't find the right syntax. Suggestions? [code] weather->yweather->location->city}"; ?> weather->current()->description()?> weather->current()->geo->lat}"?> [/code] [xml] Yahoo! Weather - Memphis, TN http://us.rd.yahoo.com/dailynews/rss/weather/Memphis__TN/*http://weather.yahoo.com/forecast/38105_f.html Yahoo! Weather for Memphis, TN en-us Mon, 04 Aug 2008 8:53 am CDT 60 Yahoo! Weather 142 18 http://weather.yahoo.com http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif Conditions for Memphis, TN at 8:53 am CDT 35.05 -90 http://us.rd.yahoo.com/dailynews/rss/weather/Memphis__TN/*http://weather.yahoo.com/forecast/38105_f.html Mon, 04 Aug 2008 8:53 am CDT
Current Conditions:
Fair, 82 F

Forecast:
Mon - Mostly Sunny. High: 99 Low: 77
Tue - Mostly Sunny. High: 100 Low: 76

Full Forecast at Yahoo! Weather
(provided by The Weather Channel)
]]>
38105_2008_08_04_8_53_CDT
[/xml]
Pádraic Brady

Re: Accessing namespaces xml/rss

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
HI Kwylez,

Your problem is that the data for yweather elements is presented as attributes rather than text containing elements. Here's how to get the current weather condition (which is a "text" attribute on the "yweather:condition" element).

Zend_Feed::registerNamespace('yweather','http://xml.weather.yahoo.com/ns/rss/1.0');
Zend_Feed::registerNamespace('geo','http://www.w3.org/2003/01/geo/wgs84_pos#');
$feed = Zend_Feed::import('http://xml.weather.yahoo.com/forecastrss?p=EIXX0014&u=c');

$condition = $feed->current()->{'yweather:condition'};
$text = $condition->getDOM()->getAttribute('text');
var_dump($text);


Notice how I access attributes by falling back on DOMDocument/DOMElement methods after retrieving the DOM representation of the correct namespaced element? If you check the RSS form Yahoo are using getting any other data should be a simple copy of the last three lines above.

Hope this helps ;)

Paddy
 
Pádraic Brady

http://blog.astrumfutura.com
http://www.patternsforphp.com
OpenID Europe Foundation Member-Subscriber


----- Original Message ----
From: kwylez <[hidden email]>
To: [hidden email]
Sent: Monday, August 4, 2008 4:14:28 PM
Subject: [fw-core] Accessing namespaces xml/rss

I am trying to parse the Yahoo! rss weather feed, specifically their values inside of namespaces, but I can't find the right path to save my life. I have registered the namespaces, but can't find the right syntax. Suggestions? [code] weather->yweather->location->city}"; ?> weather->current()->description()?> weather->current()->geo->lat}"?> [/code] [xml] Yahoo! Weather - Memphis, TN http://us.rd.yahoo.com/dailynews/rss/weather/Memphis__TN/*http://weather.yahoo.com/forecast/38105_f.html Yahoo! Weather for Memphis, TN en-us Mon, 04 Aug 2008 8:53 am CDT 60 Yahoo! Weather 142 18 http://weather.yahoo.com http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif Conditions for Memphis, TN at 8:53 am CDT 35.05 -90 http://us.rd.yahoo.com/dailynews/rss/weather/Memphis__TN/*http://weather.yahoo.com/forecast/38105_f.html Mon, 04 Aug 2008 8:53 am CDT 38105_2008_08_04_8_53_CDT [/xml]

View this message in context: Accessing namespaces xml/rss
Sent from the Zend Core mailing list archive at Nabble.com.
kwylez

Re: Accessing namespaces xml/rss

Reply Threaded More More options
Print post
Permalink
Thank you sooo much. That helps.  I will see if I can figure out the different namespaces inside of the item tag on my own.

Thanks again.

-Cory

On Mon, Aug 4, 2008 at 11:23 AM, Pádraic Brady <[hidden email]> wrote:
HI Kwylez,

Your problem is that the data for yweather elements is presented as attributes rather than text containing elements. Here's how to get the current weather condition (which is a "text" attribute on the "yweather:condition" element).

Zend_Feed::registerNamespace('yweather','http://xml.weather.yahoo.com/ns/rss/1.0');
Zend_Feed::registerNamespace('geo','http://www.w3.org/2003/01/geo/wgs84_pos#');
$feed = Zend_Feed::import('http://xml.weather.yahoo.com/forecastrss?p=EIXX0014&u=c');

$condition = $feed->current()->{'yweather:condition'};
$text = $condition->getDOM()->getAttribute('text');
var_dump($text);


Notice how I access attributes by falling back on DOMDocument/DOMElement methods after retrieving the DOM representation of the correct namespaced element? If you check the RSS form Yahoo are using getting any other data should be a simple copy of the last three lines above.

Hope this helps ;)

Paddy
 
Pádraic Brady

http://blog.astrumfutura.com
http://www.patternsforphp.com
OpenID Europe Foundation Member-Subscriber


----- Original Message ----
From: kwylez <[hidden email]>
To: [hidden email]
Sent: Monday, August 4, 2008 4:14:28 PM
Subject: [fw-core] Accessing namespaces xml/rss

I am trying to parse the Yahoo! rss weather feed, specifically their values inside of namespaces, but I can't find the right path to save my life. I have registered the namespaces, but can't find the right syntax. Suggestions? [code] weather->yweather->location->city}"; ?> weather->current()->description()?> weather->current()->geo->lat}"?> [/code] [xml] http://us.rd.yahoo.com/dailynews/rss/weather/Memphis__TN/*http://weather.yahoo.com/forecast/38105_f.html Yahoo! Weather for Memphis, TN en-us Mon, 04 Aug 2008 8:53 am CDT 60 142 18 http://weather.yahoo.com http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif 35.05 -90 http://us.rd.yahoo.com/dailynews/rss/weather/Memphis__TN/*http://weather.yahoo.com/forecast/38105_f.html Mon, 04 Aug 2008 8:53 am CDT 38105_2008_08_04_8_53_CDT [/xml]

View this message in context: Accessing namespaces xml/rss
Sent from the Zend Core mailing list archive at Nabble.com.



--
Cory Wiles
[hidden email]
http://www.corywiles.com
http://www.400mtogo.com