title: Loop over DOMDocument
body: <p>I am following the suggestion from this question Robust, <a href="https://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php/321963#321963">Mature HTML Parser for PHP</a>, about parsing html that may be malformed with <a href="http://docs.php.net/manual/en/class.domdocument.php" rel="noreferrer">DOMDocument</a>.</p>
  
  <p>Is there any easy way to loop over the parsed document?  So I would like to loop over html like this.</p>
  
  <pre><code>$html='&lt;ul&gt;
           &lt;li&gt;value1&lt;/li&gt;
           &lt;li&gt;value1&lt;/li&gt;
           &lt;li&gt;value3
              &lt;p&gt;subvalue&lt;/p&gt;
           &lt;/li&gt;
          &lt;/ul&gt;
          &lt;p&gt;hello world&lt;/p&gt;';
  
  $doc = new DOMDocument();
  $doc-&gt;loadHTML($html);
  ???
  foreach (??? as $node)
  {
    print $node-&gt;nodeName.':'.$node-&gt;nodeValue;
  }
  </code></pre>
  
  <p>And get results somewhat like this.</p>
  
  <pre><code> ul:
   li:value1
   li:value2
   li:value3
   p:subvalue
   p:hello world
  </code></pre>
  
  <p>Using <code>$doc-&gt;childNodes</code> by itself doesn't really do what I want.  Since it doesn't seem to go down to lower branches in the tree.  I used the code suggested by <a href="https://stackoverflow.com/questions/2909849/loop-over-domdocument/2909863#2909863">halfdan</a> and I get results like this.</p>
  
  <pre><code>html:
  html:value1
           value1
           value3
              subvalue
  
          hello world
  </code></pre>