2008年8月4日星期一

Re: [fw-core] Accessing namespaces xml/rss

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 <kwylez@gmail.com>
To: fw-core@lists.zend.com
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.

没有评论: