开发者

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

开发者 https://www.devze.com 2023-01-07 11:43 出处:网络
<?php $doc = new DOMDocument(); $doc->load(\'http://weather.yahooapis.com/forecastrss?p=VEXX0024&u=c\');
<?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;
        }
    }
?>

And as you can see is a simple script who return the content of the tag from the above url. The thing is that i dont need that content, i need the one who is inside the tag . I need the attributes code, temp, text. H开发者_运维百科ow do i do that simple with my actual code? Thanks

Ex of the tag content:

<yweather:condition  text="Partly Cloudy"  code="30"  temp="30"  date="Fri, 16 Jul 2010 8:30 am AST" />


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


How about:

  • DOMElement::getAttribute — Returns value of attribute

And if you need to get anything with namespaces, there is corresponding methods ending in NS.

0

精彩评论

暂无评论...
验证码 换一张
取 消