note 44735 deleted from function.dom-domdocument-savexml by didou
| From: | didou@php.net | Date: | Tue, 04 Jan 2005 10:18:36 +0000 |
| Subject: | note 44735 deleted from function.dom-domdocument-savexml by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-82681@lists.php.net to get a copy of this message | ||
Note Submitter: marcel dot kiel at gmx dot de
----
Output formatting
It is possible to output the XML code with indents and line breaks, making it more human-readable.
To activate formatting, set the property DOMDocument->formatOutput to true before calling
DOMDocument->saveXML() or DOMDocument->save().
Example:
<?php
// create the document
$doc = new DOMDocument();
// create the nodes
$root = $doc->createElement("html");
$root = $doc->appendChild($root);
$head = $doc->createElement("head");
$head = $root->appendChild($head);
$title = $doc->createElement("title");
$title = $head->appendChild($title);
$text = $doc->createTextNode("This is the title");
$text = $title->appendChild($text);
// output the XML code without formatting
print $doc->saveXML();
// output:
// <html><head><title>This is the title</title></head></html>
// activate formatting
$doc->formatOutput = true;
// output the XML code with formatting
print $doc->saveXML();
// output:
// <html>
// <head>
// <title>This is the title</title>
// </head>
// </html>
?>
Note that this option does not apply to DOMDocument->saveHTML() or
DOMDocument->saveHTMLFile().
It also does not seem to work on documents that were loaded from an XML file or string. A user
reported that output formatting could be achieved by setting the property
DOMDocument->preserveWhiteSpace to false before loading the data, but in my case, this did not
make any difference (PHP 5.0.0).
There is also a PEAR class that formats XML output without the use of DOM:
http://pear.php.net/package/XML_Beautifier
Marcel Kiel