title: Generate and save XML file
body: <p>This is what I am trying to do:</p>
  
  <p>When I click a button, a XML structure is created and a save-as dialog box 
  appears to ask the user where to save it.</p>
  
  <p>In general, everything is fine.</p>
  
  <p>The problem is: <em>the saved file contains XML structure AND js, html code</em>.</p>
  
  <p>This is how my code looks like:</p>
  
  <pre><code>/*a lot of code before */
  
  $domtree = new DOMDocument('1.0', 'UTF-8');
  $domtree-&gt;formatOutput = true;
  $xmlRoot = $domtree-&gt;createElement("xml");
  $xmlRoot = $domtree-&gt;appendChild($xmlRoot);
  //create and append other elements
  
  //save it to a file using a dialog box
  $file_name = "myFile.xml";
  header('Content-Disposition: attachment;filename=' . $file_name);
  header('Content-Type: text/xml');
  echo $domtree-&gt;saveXML();
  
  /* a lot of code after */
  </code></pre>
  
  <p>I do realize the problem could be about headers or stuff echoed before.
  But I don't know how to fix it.</p>
  
  <p>I found solutions for similar situations but it looks like the source file contains only the code that generates and saves XML file.</p>
  
  <p>In my case, I need more code around.</p>