title: Editing XML in formatted way in PHP
body: <p>I am new to PHP and I want to edit XML files using PHP. When I add child in my xml file I don't get them in proper formatted way. This is creating log of trouble in further editing in the file. Please help me edit my xml file so that the resulting xml is well formatted.</p>
  
  <p>Thanks in advance.</p>
  
  <p>I am editing my xml file using this code</p>
  
  <pre><code>&lt;?php
  
  $xml_file = simplexml_load_file('editing.xml');
  
  $xml_file-&gt;formatOutput = true;
  
  $xml_file-&gt;preserveWhiteSpace = false;
  
  $xml_file-&gt;addChild('article',' ')-&gt;addAttribute('id', 'id3');
  
  file_put_contents('editing.xml', $xml_file-&gt;asXML());
  ?&gt;
  </code></pre>
  
  <p>This is the xml file I am using.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;userva&gt;
      &lt;user id="BPL000001" first_name="f_name" last_name="l_name"&gt;
  
          &lt;article id="wersomething"&gt;
              &lt;title&gt;Some title&lt;/title&gt;
              &lt;description&gt;he does play well all the time&lt;/description&gt;
              &lt;posted_on&gt;11/Nov/2012&lt;/posted_on&gt;
              &lt;text_contents&gt;Some contents.. Some Contents..Some contents.. Some Contents..Some contents.. Some Contents..&lt;/text_contents&gt;
              &lt;images&gt;
                  &lt;image&gt;image1.jpg&lt;/image&gt;
                  &lt;image&gt;image2.jpg&lt;/image&gt;
              &lt;/images&gt;
              &lt;links&gt;
                  &lt;link&gt;link1&lt;/link&gt;
                  &lt;link&gt;link2&lt;/link&gt;
              &lt;/links&gt;
              &lt;likes&gt;
                  &lt;like&gt;like11&lt;/like&gt;
                  &lt;like&gt;like12&lt;/like&gt;
                  &lt;like&gt;like13&lt;/like&gt;
                  &lt;like&gt;like14&lt;/like&gt;
                  &lt;like&gt;like15&lt;/like&gt;
              &lt;/likes&gt;
              &lt;comments&gt;
                  &lt;comment by="523423440"/&gt;
                  &lt;comment by="054232130"/&gt;
                  &lt;comment by="15849584200"/&gt;
              &lt;/comments&gt;
         &lt;/article&gt;
  
          &lt;article id="id2"&gt;
              &lt;title&gt;Some title&lt;/title&gt;
              &lt;description&gt;he does play well all the time&lt;/description&gt;
              &lt;posted_on&gt;95956262&lt;/posted_on&gt;
              &lt;text_contents&gt;Some contents.. Some Contents..Some contents.. Some Contents..Some contents.. Some Contents..&lt;/text_contents&gt;
              &lt;images&gt;
                  &lt;image&gt;imageva1.jpg&lt;/image&gt;
                  &lt;image&gt;imageva2.jpg&lt;/image&gt;
              &lt;/images&gt;
              &lt;links&gt;
                  &lt;link&gt;link1&lt;/link&gt;
                  &lt;link&gt;link2&lt;/link&gt;
              &lt;/links&gt;
              &lt;likes&gt;
                  &lt;like&gt;like21&lt;/like&gt;
                  &lt;like&gt;like22&lt;/like&gt;
                  &lt;like&gt;like23&lt;/like&gt;
                  &lt;like&gt;like24&lt;/like&gt;
                  &lt;like&gt;like25&lt;/like&gt;
              &lt;/likes&gt;
              &lt;comments&gt;
                  &lt;comment by="0142264340"/&gt;
                  &lt;comment by="0523412130"/&gt;
                  &lt;comment by="5523484200"/&gt;
              &lt;/comments&gt;
          &lt;/article&gt;
      &lt;/user&gt;
  &lt;/userva&gt;
  </code></pre>