Monday, August 4, 2008

Access XML Nodes From Namespaces With Zend Framework

One of the requirements for a project that I am working on is that a "weather" module display on on of the main pages.  At first glance this should be pretty easy.  While working on it I noticed that I wanted to access more of the information that the feed had available.  I found plenty of tutorials on how to access the node values using SimpleXML, but I couldn't find what I was looking for utilizing Zend Framework.

After a quick response from the mailing list and references to the PHP online manual I came up with the solution.

Thanks to Pádraic Brady for the assistance.


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://weather.yahooapis.com/forecastrss?p=38105");

// Conditional Codes: http://developer.yahoo.com/weather/#codes
$condition = $feed->current()->{'yweather:condition'};
$text      = $condition->getDOM()->getAttribute('text');
print "Text {$text}
";

$astronomy = $feed->{'yweather:astronomy'};
$text      = $astronomy->getDOM()->getAttribute('sunrise');
print "Sunrise Attribute {$text}
";
          
$lat  = $feed->current()->{'geo:lat'};
$text = $lat->getDOM()->nodeValue;
print "Latitude {$text}
";

No comments: