Search code examples
phpdomdocument

Can't print_r domDocument


I only see:

DOMNodeList Object ( )

is this a php bug or something?

How am I supposed to see the HTML elements from the object?


Solution

  • When you create a DOMDocument instance, you have a PHP object. The DOM classes do not implement a helpful __toString functionality.

    To get HTML from a DOMDocument instance, you'll need to use saveHTML:

    print_r($dom->saveHTML());
    

    NB that your question suggests you are actually looking at a collection of elements (a DOMNodeList) rather than an actual DOMDocument instance. Depending on your code, you'll need to extract the code for these individually:

    foreach ($elements as $el) {
        print_r($dom->saveHTML($el)); // use saveXML if you are using a version before 5.3.6
    }