Blog Post RSS ?

Blogs » PHP » SimpleXML and namespaces
 

SimpleXML and namespaces

by Kevin Yank

There’s a lot about SimpleXML, PHP5’s new API for accessing the contents of XML documents, in SitePoint’s recently-published book No Nonsense XML Web Development With PHP, but one thing it doesn’t cover is how to use SimpleXML with a document that makes use of XML Namespaces.

Take this document, for example–a simplified RSS 1.0 feed:


<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel rdf:about="http://www.sitepoint.com/">
    <title>SitePoint.com</title>
    <link>http://www.sitepoint.com/</link>
    <description>SitePoint is the natural place to go to grow your online business.</description>
    <items>
      <rdf:Seq>
        <rdf:li rdf:resource="http://www.sitepoint.com/article/take-command-ajax" />
      </rdf:Seq>
    </items>
  </channel>
  <item rdf:about="http://www.sitepoint.com/article/take-command-ajax">
    <title>Take Command with AJAX</title>
    <link>http://www.sitepoint.com/article/take-command-ajax</link>
    <description>Want to get a bang out of your AJAX artillery?</description>
    <dc:date>2005-10-14T04:00:00Z</dc:date>
  </item>
</rdf:RDF>

In PHP5, here’s how you might think to use SimpleXML’s API to get at the date of every item in the feed:


$feed = simplexml_load_file('http://www.sitepoint.com/recent.rdf');
foreach ($feed->item as $item) {
  echo $item->date;
}

But this won’t work, because the date element has a namespace prefix (<dc:date>), so it can’t be accessed by the usual means.

Here’s the solution. First, check what the URI is for the namespace. In this case, the dc: prefix maps to the URI http://purl.org/dc/elements/1.1/:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/">

Then use the children method of the SimpleXML object, passing it that URI:


$feed = simplexml_load_file('http://www.sitepoint.com/recent.rdf');
foreach ($feed->item as $item) {
  $ns_dc = $item->children('http://purl.org/dc/elements/1.1/');
  echo $ns_dc->date;
}

When you pass the namespace URI to the children method, you get a SimpleXML collection of the child elements belonging to that namespace. You can work with that collection the same way you would with any SimpleXML collection.

You can use the attributes method in the same way to obtain attributes with namespace prefixes.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Ping.fm
  • Twitthis

Related posts:

  1. How to Use PHP Namespaces, Part 1: The Basics In the first part of a series of articles, Craig...
  2. How to Use PHP Namespaces, Part 3: Keywords and Autoloading In the final part of his series explaining PHP namespaces,...
  3. How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution In the second part of Craig's PHP namespaces series, he...
  4. Are PHP Namespaces Really So Bad? Namespaces have caused a divide amongst developers who either love...
  5. Poll: Is RSS Mainstream? Could RSS be crossing over into the mainstream? New data...

This post has 18 responses so far

Sponsored Links

SitePoint Marketplace

Buy and sell Websites, templates, domain names, hosting, graphics and more.

Follow SitePoint on...