Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <em>SimpleXML</em> extension is limited to format the output, it's sister extension, <a href="http://php.net/DOMDocument" rel="nofollow noreferrer"><em>DOMDocument</em></a> has support for output formatting. The XML string from your example and making use of <code>DOMDocument::$preserveWhiteSpace</code> and <code>DOMDocument::$formatOutput</code> to control the formatttings:</p> <pre><code>$doc = new DOMDocument(); $doc-&gt;preserveWhiteSpace = false; $doc-&gt;formatOutput = true; $doc-&gt;loadXML($string); echo $doc-&gt;saveXML(); </code></pre> <p>This will output a nicely indented XML with the linebreaks where you have asked for them:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;items&gt; &lt;item&gt; &lt;title&gt;title3&lt;/title&gt; &lt;desc&gt;This is some desc3&lt;/desc&gt; &lt;/item&gt; &lt;empty/&gt; &lt;/items&gt; </code></pre> <p>If you further need to manipulate the indent, you can make use of regular expressions which has been outlined in a related question and answer: <strong><em><a href="https://stackoverflow.com/q/8616594/367456">Converting indentation with preg_replace (no callback)</a></em></strong>.</p> <p>If you don't want to use that method you could also switch from SimpleXML to something else and then to XMLWriter which provides a method to set the indentation (<a href="http://www.php.net/manual/en/function.xmlwriter-set-indent.php" rel="nofollow noreferrer">XMLWriter::setIndent</a>) of printed XML. You would need to find an interim representation of your XML model to write it with <code>XMLWriter</code> however which does not look that trivial.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload