如何从PHP的Yahoo Weather RSS获取标签"<yweather:condition>"?

Dom*_*oSL 1 php yahoo-weather-api

<?php
    $doc = new DOMDocument();
    $doc->load('http://weather.yahooapis.com/forecastrss?p=VEXX0024&u=c');
    $channel = $doc->getElementsByTagName("channel");
    foreach($channel as $chnl)
    {
        $item = $chnl->getElementsByTagName("item");
        foreach($item as $itemgotten)
        {
            $describe = $itemgotten->getElementsByTagName("description");
            $description = $describe->item(0)->nodeValue;
            echo $description;
        }
    }
?>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,这是一个简单的脚本,它从上面的url返回标记的内容.问题是我不需要那些内容,我需要标签内的人.我需要属性代码,temp,text.我如何用我的实际代码做到这一点?谢谢

标签内容的前缀:

<yweather:condition  text="Partly Cloudy"  code="30"  temp="30"  date="Fri, 16 Jul 2010 8:30 am AST" />
Run Code Online (Sandbox Code Playgroud)

Art*_*cto 5

就像是:

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

关键是你必须使用getElementsByTagNameNS而不是getElementsByTagName并指定"http://xml.weather.yahoo.com/ns/rss/1.0"为命名空间.

您知道这yweather是一个快捷方式,http://xml.weather.yahoo.com/ns/rss/1.0因为XML文件包含一个xmls属性:

<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#">
Run Code Online (Sandbox Code Playgroud)