title: Format XML Document created with PHP - DOMDocument
body: <p>I am trying to format visually how my XML file looks when it is output.  Right now if you go <a href="http://www.levijackson.net/test/crawl-curl/sitemap.xml" rel="nofollow noreferrer">here</a> and view the source you will see what the file looks like.</p>
  <p>The PHP I have that creates the file is: (Note, $links_array is an array of urls)</p>
  <pre><code>        header('Content-Type: text/xml');
          $sitemap = new DOMDocument;
          
          // create root element
          $root = $sitemap-&gt;createElement(&quot;urlset&quot;);
          $sitemap-&gt;appendChild($root);
           
          $root_attr = $sitemap-&gt;createAttribute('xmlns'); 
          $root-&gt;appendChild($root_attr); 
  
          $root_attr_text = $sitemap-&gt;createTextNode('http://www.sitemaps.org/schemas/sitemap/0.9'); 
          $root_attr-&gt;appendChild($root_attr_text); 
  
          
          
          foreach($links_array as $http_url){
          
                  // create child element
                  $url = $sitemap-&gt;createElement(&quot;url&quot;);
                  $root-&gt;appendChild($url);
                  
                  $loc = $sitemap-&gt;createElement(&quot;loc&quot;);
                  $lastmod = $sitemap-&gt;createElement(&quot;lastmod&quot;);
                  $changefreq = $sitemap-&gt;createElement(&quot;changefreq&quot;);
                  
                  $url-&gt;appendChild($loc);
                  $url_text = $sitemap-&gt;createTextNode($http_url);
                  $loc-&gt;appendChild($url_text);
                  
                  $url-&gt;appendChild($lastmod);
                  $lastmod_text = $sitemap-&gt;createTextNode(date(&quot;Y-m-d&quot;));
                  $lastmod-&gt;appendChild($lastmod_text);
                  
                  $url-&gt;appendChild($changefreq);
                  $changefreq_text = $sitemap-&gt;createTextNode(&quot;weekly&quot;);
                  $changefreq-&gt;appendChild($changefreq_text);
                  
          }
          
          $file = &quot;sitemap.xml&quot;;
          $fh = fopen($file, 'w') or die(&quot;Can't open the sitemap file.&quot;);
          fwrite($fh, $sitemap-&gt;saveXML());
          fclose($fh);
      }
  </code></pre>
  <p>As you can tell by looking at the source, the file isn't as readable as I would like  it to be.  Is there any way for me to format the nodes?</p>
  <p>Thanks,<br>
  Levi</p>