3

I only see:

DOMNodeList Object ( )

is this a php bug or something?

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

Charles
  • 50,943
  • 13
  • 104
  • 142
Anda
  • 133
  • 1
  • 2
  • 5
  • Not an exact duplicate, but connected one: http://stackoverflow.com/questions/4776093/why-var-dump-cant-print-domdocument-object-only-with-printdom-savehtml-its – jakub.g Sep 17 '11 at 22:37

1 Answers1

7

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
}
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • great.. are there any other classes like domdocument that work with print_r( ? – Anda Sep 17 '11 at 22:24
  • I can't believe that `print_r` is such a major part of your application that you have to choose what library you are using around it. Why do you need this? – lonesomeday Sep 17 '11 at 22:25
  • 1
    well i cant see anything in the object, and I dont know what elements to remove and what not... I tried `$tag->parentNode->saveHTML()` and it doesnt work.. I get some fatal error. This has to be like the most stupid class ever. there's no remove element method etc. – Anda Sep 17 '11 at 22:26
  • That's because you need to call `saveHTML` on the DOMDocument instance, as I show in my code above. You can use `$el->ownerDocument->saveHTML($el)` if you don't have a reference to it locally. It's not stupid, but the DOM can be a little tricky to get your head around. It's worth the work. – lonesomeday Sep 17 '11 at 22:28