← back to code snippets


RSS namespaces

Posted on August 4th, 2014 by Graeme

Here we use a sample bit of RSS that contains two thumbnails, which belong to a namespace. Extracting the namespaced data is slightly different than what you would do normally..

<rss xmlns:media=“http://whatever.com/rss”>

<item>

<media:thumbnail width=“100” height=“100” url=“www.whatever.com/image1.jpg”/>

<media:thumbnail width=“150” height=“150” url=“www.whatever.com/image2.jpg”/>

</item>

The above thumbnail is part of the media namespace, which is defined at the top by the rss tag. To get the media item, you’d do something like this:

  • First we use the children method with the namespace url as the argument:

$media = $item->children(“http://whatever.com/rss”)

Then to get the first media element, do this:

print_r($media->thumbnail[0]->attributes());

We use the attributes method because all the information about the media elements in the RSS feed are simply defined as attributes within the media tag.