Question

I want to know the temperature from a city by using Yahoo Wheater (xml), xpath and php. This is the code that I use but it doesn't work. I can get an element (description) but not the attribute (temperature) that I want.

$xml = new DOMDocument();
$xml->load("http://weather.yahooapis.com/forecastrss?w=12818432&u=c");

$xpath = new DOMXpath($xml);
$result = $xpath->query("//channel");

foreach ($result as $value) {
    //$weather = $value->getElementsByTagName("description");
    $weather = $value->getElementsByTagName("yweather:wind");
    $temp = $weather->getAttribute("chill");

    print $temp->item(0)->textContent;
}
Was it helpful?

Solution

You should use getElementsByTagNameNS() to get elements within a namespace.

$ns_yweather = "http://xml.weather.yahoo.com/ns/rss/1.0";
$weer = $value->getElementsByTagNameNS($ns_yweather, "wind")->item(0);
print $weer->getAttribute("chill");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top