How to Get the Tag "<Yweather:Condition>" from Yahoo Weather Rss in PHP

How to get the tag yweather:condition from Yahoo Weather RSS in PHP?

Something like:

echo $itemgotten->getElementsByTagNameNS(
"http://xml.weather.yahoo.com/ns/rss/1.0","condition")->item(0)->
getAttribute("temp");

The key is that you have to use getElementsByTagNameNS instead of getElementsByTagName and specify "http://xml.weather.yahoo.com/ns/rss/1.0" as the namespace.

You know yweather is a shortcut for http://xml.weather.yahoo.com/ns/rss/1.0 because the XML file includes a xmls attribute:

<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#">

yahoo weather Api

This seems pretty straightforward. From the tutorial:

Since the only bit of information we care about is the yweather:condition element's text attribute, we're going to avoid creating an XML parsing object and use a short regular expression.

So, just look at the line with the regular expression:

$weather_class = format_result(
get_match( '/<yweather:condition text="(.*)"/isU', $data )
);

This is actually a bad regular expression because it assumes text will always be the first attribute (and that there'll always be that weird double-space. Here's a regular expression that will get the temp attribute regardless of where it falls:

/<yweather:condition\s+(?:.*\s)?temp="(.+)"/isU

Substitute that for the regular expression given to get_match() and you should be good to go.

Oh, and lest I be kicked off SO for not saying so: Attempting to parse arbitrary HTML XML with regular expressions is the path to madness.

Yahoo Weather API: Get data from another day

It doesn't look like the Yahoo API supports extended forecasts. Take a look at the Weather Underground API, they have a 10 day forecast available.

Yahoo Weather Rss Reading with ASP.NET MVC?

The following expression should work, first reference the ycweather namespace;

XNamespace yWeatherNS = "http://xml.weather.yahoo.com/ns/rss/1.0";

then you get the attribute values this way:

Text = feed.Element(yWeatherNS + "condition").Attribute("text").Value

The problem is that your condition element in in another namespace, so you must select this node in the context of that namespace via the XNamespace.

You can read more about XML Namespaces via the MSDN article Namespaces in C#

PHP and simplexml_load_file()

There is a very good info at the said link..



Related Topics



Leave a reply



Submit