Skip to main content

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=""
    xmlns="" xmlns:dc="">
  <channel rdf:about="">
    <description>SitePoint is the natural place to go to grow your online business.</description>
        <rdf:li rdf:resource="" />
  <item rdf:about="">
    <title>Take Command with AJAX</title>
    <description>Want to get a bang out of your AJAX artillery?</description>

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('');
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

<rdf:RDF xmlns:rdf=""
    xmlns="" xmlns:dc="">

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

$feed = simplexml_load_file('');
foreach ($feed->item as $item) {
  $ns_dc = $item->children('');
  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.

Kevin Yank is an accomplished web developer, speaker, trainer and author of Build Your Own Database Driven Website Using PHP & MySQL and Co-Author of Simply JavaScript and Everything You Know About CSS is Wrong! Kevin loves to share his wealth of knowledge and it didn't stop at books, he's also the course instructor to 3 online courses in web development. Currently Kevin is the Director of Front End Engineering at Culture Amp.

Integromat Tower Ad