PHP: Parsing SimpleXML nodes with namespaces

0 Comments

SimpleXML is one of the easiest parsers to use, but when it comes to namespaces, things get just a little bit tricker.

You'll run into them when you're parsing media RSS feeds such as:

  • FlickR (media:thumbnail, media:category, media:content)
  • YouTube API (yt:duration, yt:statistics, gd:rating, gd:comments)
  • or Yahoo Weather (yweather:location, yweather:astronomy)

They might be a bit gay to deal with, but luckily it isn't too much harder.

9
Namespaces, almost as gay as this guy.

The trick is having to gather the namespace children before pulling the element you want.

Using this short sample XML from FlickR.

01.<?xml version="1.0" encoding="utf-8"?>
02.  <rss version="2.0"
03.    xmlns:media="http://search.yahoo.com/mrss/"
04.    xmlns:dc="http://purl.org/dc/elements/1.1/"
06.    xmlns:flickr="urn:flickr:">
07.  <channel>
08.    <item>
09.      <title>発売中</title>
10.      ...
11.      <dc:date.Taken>2010-06-13T13:08:52-08:00</dc:date.Taken>
12.      <media:thumbnail url="http://farm6.static.flickr.com/5122/5370686057_cb21430ac5_s.jpg" height="75" width="75" />
13.      ...
14.    </item>
15.  </channel>
16.</rss>

You can use this PHP code to extract the "media:thumbnail" elements from the RSS item.

01.$entries = simplexml_load_file('http://api.flickr.com/services/feeds/photos_public.gne?tags=tokyo&;format=rss_200');
02.$namespaces = $entries->getNamespaces(true);
03. 
04.foreach ($entries->channel->item as $feeditem) {
05.  $thumbnail = $feeditem->children($namespaces['media'])->thumbnail;
06.  $attr = $thumbnail->attributes();
07. 
08.  echo '<pre>';
09.  echo "URL = {$attr['url']}, width = {$attr['width']}, height = {$attr['height']}\n";
10.  echo print_r($attr, true);
11.  echo '</pre>';
12.}

The code was based off the source from PHPFreaks, but modified to avoid using the URI of namespaces as it makes the code ugly.

Enjoy!

[ Source ]

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog