PHP Xml How to Output Nice Format

PHP XML how to output nice format

You can try to do this:

...
// get completed xml document
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$xml_string = $doc->saveXML();
echo $xml_string;

You can make set these parameter right after you've created the DOMDocument as well:

$doc = new DomDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;

That's probably more concise. Output in both cases is (Demo):

<?xml version="1.0"?>
<root>
<error>
<a>eee</a>
<b>sd</b>
<c>df</c>
</error>
<error>
<a>eee</a>
<b>sd</b>
<c>df</c>
</error>
<error>
<a>eee</a>
<b>sd</b>
<c>df</c>
</error>
</root>

I'm not aware how to change the indentation character(s) with DOMDocument. You could post-process the XML with a line-by-line regular-expression based replacing (e.g. with preg_replace):

$xml_string = preg_replace('/(?:^|\G)  /um', "\t", $xml_string);

Alternatively, there is the tidy extension with tidy_repair_string which can pretty print XML data as well. It's possible to specify indentation levels with it, however tidy will never output tabs.

tidy_repair_string($xml_string, ['input-xml'=> 1, 'indent' => 1, 'wrap' => 0]);

xml formatting- simple xml does not do pretty formatting (indent formatting)

First of all you were successful in finding out that DOMDocument supports formatted XML while SimpleXMLElement does not. And that is totally correct.

So as you want to use SimpleXMLElement to actually create the XML document but you also want to benefit from the formatting options from DOMDocument you might want to learn about the fact that both libraries are sister-libraries that work very well with each other. You already sort of seem to know about that as you make use of simplexml_import_dom to convert a DOMNode object into it's SimpleXMLElement object.

You however seem to miss how near you already were. Please see the following example which I copied from your question - I just added a single line at the very end (and change the filename to standard output so it echoes out the documents):

$dom = new domDocument;

$dom->formatOutput = true;
$root = $dom->appendChild($dom->createElement( "user" ));
$sxe = simplexml_import_dom( $dom );
$sxe->addChild("firstname", "John");
$sxe->addChild("surname", "Brady");

$sxe->asXML('php://output');
$dom->save('php://output');

The SimpleXMLElement::saveXML() method will use non-formatted output while the DOMDocument::save() method will use the formatting as you set the parameters for:

<?xml version="1.0"?>
<user><firstname>John</firstname><surname>Brady</surname></user>
<?xml version="1.0"?>
<user>
<firstname>John</firstname>
<surname>Brady</surname>
</user>

And that's it already.

Format output of $SimpleXML->asXML();

There's a variety of solutions in the comments on the PHP manual page for SimpleXMLElement. Not very efficient, but certainly terse, is a solution by Anonymous

$dom = dom_import_simplexml($simpleXml)->ownerDocument;
$dom->formatOutput = true;
echo $dom->saveXML();

The PHP manual page comments are often good sources for common needs, as long as you filter out the patently wrong stuff first.

PHP - Format XML Output

Use the DOM library and set the formatOutput property to true

$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;

$root = $doc->createElement('error');
$doc->appendChild($root);

$desc = $doc->createElement('description', $error);
$root->appendChild($desc);

echo $doc->saveXML();

Demo ~ https://eval.in/461384

Prettifying/Formatting output in SimpleXML

AFAIK simpleXML can't do it on its own.

However, DOMDocument can.

$dom = dom_import_simplexml($sxe)->ownerDocument;
$dom->formatOutput = TRUE;
$formatted = $dom->saveXML();


Related Topics



Leave a reply



Submit