title: Problems parsing Facebook atom feed using PHP
body: <p>I have problems parsing a atom facebook feed.</p>
  
  <p>Using:</p>
  
  <p>PHP's DOMDocument.</p>
  
  <p>Errors:</p>
  
  <blockquote>
    <p>Warning: DOMDocument::loadXML(): xmlParseEntityRef: no name in Entity, line: 12
    Warning: DOMDocument::loadXML(): xmlParseEntityRef: no name in Entity, line: 12
    Warning: DOMDocument::loadXML(): Entity 'euro' not defined in Entity, line: 16
    Warning: DOMDocument::loadXML(): Entity 'acute' not defined in Entity, line: 16
    Warning: DOMDocument::loadXML(): Entity 'euro' not defined in Entity, line: 16
    Warning: DOMDocument::loadXML(): Entity 'acute' not defined in Entity, line: 16
    Notice: Trying to get property of non-object in ... on line 76</p>
  </blockquote>
  
  <p>Unfortunately, the entities mentioned above cannot be found in feed code. Not that simple... The problem has to be something different. Other feeds can be parsed using the same code without any problems. So, I think the problem is something in Facebook's HTML inside the content tag. <strong><em>What could this be? How to solve?</em></strong></p>
  
  <pre><code>    &lt;content type="html"&gt;&amp;lt;div class=&amp;quot;ext_media clearfix has_extra has_thumb&amp;quot;&amp;gt;&amp;lt;div class=&amp;quot;title&amp;quot;&amp;gt;&amp;lt;a href=&amp;quot;http://www.youtube.com/watch?v=BPq58p0K6DM&amp;amp;feature=youtu.be&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;nofollow&amp;quot; title=&amp;quot;http://www.youtube.com/watch?v=BPq58p0K6DM&amp;amp;amp;feature=youtu.be&amp;quot; onmousedown=&amp;quot;UntrustedLink.bootstrap($(this), &amp;quot;-AQBiGfHA&amp;quot;, event, bagof(null));&amp;quot;&amp;gt;Did you know there were this many satellites in orbit VIDEO&amp;lt;/a&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div class=&amp;quot;url&amp;quot;&amp;gt;Quelle: www.youtube.com&amp;lt;/div&amp;gt;&amp;lt;div class=&amp;quot;story_posted_item clearfix&amp;quot;&amp;gt;&amp;lt;div class=&amp;quot;extra&amp;quot;&amp;gt;&amp;lt;div class=&amp;quot;share_thumb&amp;quot;&amp;gt;&amp;lt;a href=&amp;quot;http://www.youtube.com/watch?v=BPq58p0K6DM&amp;amp;feature=youtu.be&amp;quot; target=&amp;quot;_blank&amp;quot; rel=&amp;quot;nofollow&amp;quot; onmousedown=&amp;quot;UntrustedLink.bootstrap($(this), &amp;quot;2AQBCjOTV&amp;quot;, event, bagof(null));&amp;quot;&amp;gt;&amp;lt;img class=&amp;quot;img_loading img&amp;quot; src=&amp;quot;http://i3.ytimg.com/vi/BPq58p0K6DM/default.jpg&amp;quot; alt=&amp;quot;&amp;quot; onload=&amp;quot;var img = this; onloadRegister(function() { adjustImage(img); });&amp;quot; id=&amp;quot;share_thumb_257759307568958&amp;quot; /&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&lt;/content&gt;
  </code></pre>
  
  <p><strong>See the full feed code</strong> <a href="https://www.evernote.com/shard/s6/sh/d0005b72-e279-493d-8cf4-11a109206167/50f87488a411b7cbe3c5a37fa0096e2e" rel="nofollow">here</a>. (See feed <a href="http://www.facebook.com/feeds/share_posts.php?id=1168014008&amp;viewer=1168014008&amp;key=AWgP6vga8rulmfoS&amp;format=atom10" rel="nofollow">in browser</a>.)</p>
  
  <p>Full PHP code that I am using:</p>
  
  <pre><code>    $feed_xml_str = ...;
  
  print '&lt;pre&gt;';
  print_r( xmlstr_to_array($feed_xml_str) );
  print '&lt;/pre&gt;';
  
  function xmlstr_to_array($xmlstr) {
      $doc = new DOMDocument();
      $doc-&gt;loadXML($xmlstr);
      return domnode_to_array($doc-&gt;documentElement);
  }
  function domnode_to_array($node) {
      $output = array();
      switch ($node-&gt;nodeType) {
          case XML_CDATA_SECTION_NODE:
          case XML_TEXT_NODE:
              $output = trim($node-&gt;textContent);
              break;
          case XML_ELEMENT_NODE:
              for ($i=0, $m=$node-&gt;childNodes-&gt;length; $i&lt;$m; $i++) {
                  $child = $node-&gt;childNodes-&gt;item($i);
                  $v = domnode_to_array($child);
                  if(isset($child-&gt;tagName)) {
                      $t = $child-&gt;tagName;
                      if(!isset($output[$t])) {
                          $output[$t] = array();
                      }
                      $output[$t][] = $v;
                  }
                  elseif($v) {
  
                      // &gt;&gt;&gt;&gt;&gt; WJ: OUT COMMENTED CODE &gt;&gt;&gt;&gt;&gt;
                      //$output = (string) $v;
                      // &gt;&gt;&gt;&gt;&gt; WJ: ADDED CODE &gt;&gt;&gt;&gt;&gt;
                      if($node-&gt;attributes-&gt;length) {
                          $a = array();
                          foreach($node-&gt;attributes as $attrName =&gt; $attrNode) {
                              $a[$attrName] = (string) $attrNode-&gt;value;
                          }
                          $output['@attributes'] = $a;
                          $output['@value'] = (string) $v;
                      }
                      else
                          $output = (string) $v;
                      // &gt;&gt;&gt;&gt;&gt; WJ: MODIFIED CODE END &gt;&gt;&gt;&gt;&gt;
  
                  }
              }
              if(is_array($output)) {
                  if($node-&gt;attributes-&gt;length) {
                      $a = array();
                      foreach($node-&gt;attributes as $attrName =&gt; $attrNode) {
                          $a[$attrName] = (string) $attrNode-&gt;value;
                      }
                      $output['@attributes'] = $a;
                  }
                  foreach ($output as $t =&gt; $v) {
                      if(is_array($v) &amp;&amp; count($v)==1 &amp;&amp; $t!='@attributes') {
                          $output[$t] = $v[0];
                      }
                  }
              }
              break;
      }
      return $output;
  }
  </code></pre>