Alex Rivera | Logout

Loop over DOMDocument

Asked 2010-05-26T02:51:54.150
27

I am following the suggestion from this question Robust, Mature HTML Parser for PHP, about parsing html that may be malformed with DOMDocument.

Is there any easy way to loop over the parsed document? So I would like to loop over html like this.

$html='<ul>
         <li>value1</li>
         <li>value1</li>
         <li>value3
            <p>subvalue</p>
         </li>
        </ul>
        <p>hello world</p>';

$doc = new DOMDocument();
$doc->loadHTML($html);
???
foreach (??? as $node)
{
  print $node->nodeName.':'.$node->nodeValue;
}

And get results somewhat like this.

 ul:
 li:value1
 li:value2
 li:value3
 p:subvalue
 p:hello world

Using $doc->childNodes 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 halfdan and I get results like this.

html:
html:value1
         value1
         value3
            subvalue

        hello world
Edit
Report

2 Answers

46

Try this:

$doc = new DOMDocument();
$doc->loadHTML($html);
showDOMNode($doc);

function showDOMNode(DOMNode $domNode) {
    foreach ($domNode->childNodes as $node)
    {
        print $node->nodeName.':'.$node->nodeValue;
        if($node->hasChildNodes()) {
            showDOMNode($node);
        }
    }    
}
answered 2010-05-26T02:59:11.487
2

I was having issues with elements that had c data, where even elements that didn't have children where returning that they did.

I am not sure why it was.

The work around I found was to change

if($node->hasChildNodes()) {
        showDOMNode($node);
    }

to

if($node->childNodes->length != 1) {
        showDOMNode($node);
    }

And the code now works perfectly.

answered 2012-10-23T06:05:10.920

Your Answer