title: PHP DOM append child with new line
body: <p>My name Rithy.</p>
  
  <p>I don't know how to add new line before appending a new node or element in the xml.</p>
  
  <p><strong>My php:</strong></p>
  
  <pre><code>$dom = new DOMDocument();
  
  $dom-&gt;formatOutput = true;
  
  $dom-&gt;preserveWhiteSpace = true;
  
  $dom-&gt;load($xml_file);
  
  $body = $dom-&gt;getElementsByTagName('body')-&gt;item(0);
  
  $newelement_seg = $dom-&gt;createElement('seg');
  
  $data = $dom-&gt;createTextNode(" text 2 ");  
  
  $newelement_seg-&gt;appendChild($data);
  
  $body-&gt;appendChild($newelement_seg);
  
  $dom-&gt;save($xml_file);
  </code></pre>
  
  <p><strong>XML Before append a new child:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;body&gt;
      &lt;seg&gt;
          text 1
      &lt;/seg&gt;
  &lt;/body&gt;
  &lt;/xml&gt;
  </code></pre>
  
  <p><strong>XML after append a new child:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;body&gt;
      &lt;seg&gt;
          text 1
      &lt;/seg&gt;
      &lt;seg&gt;
          text 2
      &lt;/seg&gt;
  &lt;/body&gt;
  &lt;/xml&gt;
  </code></pre>
  
  <p><strong>But I want:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;body&gt;
          &lt;seg&gt;
              text 1
          &lt;/seg&gt;
          &lt;seg&gt;
              text 2
          &lt;/seg&gt;
      &lt;/body&gt;
  &lt;/xml&gt;
  &lt;hr/&gt;
  </code></pre>
  
  <p>Thanks in advance!</p>