It took some searching to figure this one out. I didn't see much in the way of explaining this glitch in the manual thus far. (For PHP5 I believe)
formatOutput = true; appears to fail when the origin of the DOM came from a file via load(). EX:
<?php
$dom = new DOMDocument();
$dom->load ("test.xml");
$dom->formatOutput = true;
$new_tag = $dom->createElement ('testNode');
$new_tag->appendChild (
$dom->createElement ('test', 'this is a test'));
$dom->documentElement->appendChild ($new_tag);
printf ("<pre>%s</pre>", htmlentities ($dom->saveXML()));
?>
Will not indent the output and will display the modified nodes all in one long line. Makes for editing a config.xml a bit difficult when saving to a file.
By adding the preserveWhiteSpace = false; BEFORE the load() the formatOutput works as expected. EX:
<?php
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->load ("test.xml");
$dom->formatOutput = true;
$new_tag = $dom->createElement ('testNode');
$new_tag->appendChild (
$dom->createElement ('test', 'this is a test'));
$dom->documentElement->appendChild ($new_tag);
printf ("<pre>%s</pre>", htmlentities ($dom->saveXML()));
?>
CAUTION: If your loaded xml file (test.xml) has an empty root node that is not shortened or has no children this will NOT work.
Example:
DOES NOT WORK:
<?xml version="1.0"?>
<root>
</root>
WORKS:
<?xml version="1.0"?>
<root/>
WORKS:
<?xml version="1.0"?>
<root>
<!-- comment -->
</root>
WORKS:
<?xml version="1.0"?>
<root>
<child/>
</root>