Using SimpleXML to read RSS feed

As you already know, SimpleXML lets you select an node’s child using the object property operator -> or a node’s attribute using the array access ['name']. It’s great, but the operation only works if what you select belongs to the same namespace.

If you want to “hop” from a namespace to another, you can use the children() or attributes() methods. In your case, this is made a bit trickier because you have <item/> in the global namespace, the node you’re looking for is in the “media” namespace* and then the attributes are in the global namespace again (they are not prefixed.) So using the normal object/array notation you’ll have to “hop” twice:

foreach ($rss->channel->item as $item)
{
    // we load the attributes into $thumbAttr
    // you can either use the namespace prefix
    $thumbAttr = $item->children('media', true)->thumbnail->attributes();

    // or preferably the namespace name, read note below for an explanation
    $thumbAttr = $item->children('http://search.yahoo.com/mrss/')->thumbnail->attributes();

    echo $thumbAttr['url'];
}

*Note

I refer to the namespace as the “media” namespace but that’s not really correct. The namespace name is http://search.yahoo.com/mrss/, and “media” is just a prefix, some sort of alias if you will. What’s important to keep in mind is that http://search.yahoo.com/mrss/ is the real name of the namespace. At some point, your RSS provider might decide to change the prefix to, say, “yahoo” and your script will stop working if your script refers to the “media” prefix. However, if you use the namespace name, it will keep working no matter the prefix.

Leave a Comment