Simple XML - Dealing With Colons In Nodes

I'm trying to read an RSS feed from Flickr but it has some nodes which are not readable by Simple XML (media:thumbnail, flickr:profile, and so on).

How do I get round this? My head hurts when I look at the documentation for the DOM. So I'd like to avoid it as I don't want to learn.

I'm trying to get the thumbnail by the way.


The solution is explained in this nice article. You need the children() method for accessing XML elements which contain a namespace. This code snippet is quoted from the article:

$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; 
}

With the latest version, you can now reference colon nodes with curly brackets.

$item->{'itunes:duration'}

You're dealing with a namespace? I think you need to use the ->children method.

$ns_dc = $item->children('http://namespace.org/');

Can you provide a snippet with the xml declaration?


An even simpler method using PHP of accessing namespaced XML nodes without declaring a namespace is....

In order to get the value of <su:authorEmail> from the following source

<item>
  <title>My important article</title>
  <pubDate>Mon, 29 Feb 2017 00:00:00 +0000</pubDate>
  <link>https://myxmlsource.com/32984</link>
  <guid>https://myxmlsource.com/32984</guid>
  <author>Blogs, Jo</author>
  <su:departments>
    <su:department>Human Affairs</su:department>
  </su:departments>
  <su:authorHash>4f329b923419b3cb2c654d615e22588c</su:authorHash>
  <su:authorEmail>hIwW14tLc+4l/oo7agmRrcjwe531u+mO/3IG3xe5jMg=</su:authorEmail>
  <dc:identifier>/32984/Download/0032984-11042.docx</dc:identifier>
  <dc:format>Journal article</dc:format>
  <dc:creator>Blogs, Jo</dc:creator>
  <slash:comments>0</slash:comments>
</item>

Use the following code:

$rss = new DOMDocument();

$rss->load('https://myxmlsource.com/rss/xml');

$nodes = $rss->getElementsByTagName('item');

foreach ($nodes as $node) {
    $title = $node->getElementsByTagName('title')->item(0)->nodeValue;
    $author = $node->getElementsByTagName('author')->item(0)->nodeValue;
    $authorHash = $node->getElementsByTagName('authorHash')->item(0)->nodeValue;
    $department = $node->getElementsByTagName('department')->item(0)->nodeValue;
    $email = decryptEmail($node->getElementsByTagName('authorEmail')->item(0)->nodeValue);
}