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

    $doc = new DOMDocument();
    $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. How 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" />

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Something like:

echo $itemgotten->getElementsByTagNameNS(

The key is that you have to use getElementsByTagNameNS instead of getElementsByTagName and specify "" as the namespace.

You know yweather is a shortcut for because the XML file includes a xmls attribute:

<rss version="2.0" xmlns:yweather=""

Solution 2

How about:

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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply