note 68892 added to function.dom-domdocument-savexml

From: Date: Tue, 15 Aug 2006 05:52:01 +0000
Subject: note 68892 added to function.dom-domdocument-savexml
Groups: php.notes 
Request: Send a blank email to php-notes+get-115922@lists.php.net to get a copy of this message
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> ---- Server IP: 64.71.164.2 Probable Submitter: 216.255.172.254 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.dom-domdocument-savexml.php Edit -- https://master.php.net/note/edit/68892 Del: integrated -- https://master.php.net/note/delete/68892/integrated Del: useless -- https://master.php.net/note/delete/68892/useless Del: bad code -- https://master.php.net/note/delete/68892/bad+code Del: spam -- https://master.php.net/note/delete/68892/spam Del: non-english -- https://master.php.net/note/delete/68892/non-english Del: in docs -- https://master.php.net/note/delete/68892/in+docs Del: other reasons-- https://master.php.net/note/delete/68892 Reject -- https://master.php.net/note/reject/68892 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#115922) next »