DOM返回空对象

This is my code:

<?php
$content = utf8_encode( file_get_contents( "data.xml") );
$XML = new DOMDocument();
$XML->loadXML($content);

echo "<pre>" . print_r($XML, 1) . "</pre>";
?>

It does return:

 DOMDocument Object
(
)

I've read its a bug or something, since both var_dump and print_r or any will return an empty object element.

http://www.php.net/manual/en/class.domelement.php#86224

This guy gave an solution to better use:

$xml = $XML->saveXML($XML);
echo "<pre>" . print_r($xml, 1) . "</pre>";

But then, when I'm reloading the page, it looks like there are only spaces, because I can't see anything, but I can select it...

Where is the problem, have you guys any solutions for that?

A var_dump on DOMDocument always returns an empty object.

Illustration:

$xml = simplexml_load_file('test.xml'); //load your xml

var_dump($xml); //var_dump via SimpleXML (lot of data)

$doc = new DOMDocument(); //fire up domdocument

$xmlstring = $xml->asXML(); //convert SimpleXML to string

$doc->loadXML($xmlstring);  //loads perfectly 

var_dump($doc); //empty object

Maybe you should consider SimpleXML?

I've experienced the same problem. It seems that the HTML validation code is very sensitive to errors. If you turn on warnings, you'll probably see a bunch of warnings when the HTML content is loaded (at least that was the case for me -loading up a remote page).

I've found that it particularly gets hung up on HTML entities. It also seems to get caught up on HTML tags compiled in javascript.

I attempted to hack through it by replacing &'s with &amp;'s just to see if I'd be able to get a properly parsed object back. I continued to get errors on JS though.

Basically, make sure you have debugging output visible and check for warnings. The content might end up being the issue here.

Edit: I did a little searching and have found this library which seems to opperate very similarly to DOMDocument, but doesn't appear to explode on small issues in the HTML: http://simplehtmldom.sourceforge.net/

I was able to load up the web page I was having problems with -without error. Might be worth a shot in your future projects.

What the author forgot to add was htmlspecialchars():

$xml = $XML->saveXML($XML);
echo "<pre>", htmlspecialchars($xml, ENT_QUOTES, 'UTF-8'), "</pre>";

Without it, the XML output doesn't get escaped and you won't see the tags.