title: How to write an XML to file, with just a Parser instance?
body: <p>This is my Groovy code:</p>
  
  <pre><code>// ...
  def xml = new XmlParser().parse(fileName)
  xml.each { e -&gt;
    // some changes are being made to XML
    xml.append("test", "test me")
  }
  def writer = new FileWriter(newFileName)
  new XmlNodePrinter(new PrintWriter(writer)).print(xml)
  </code></pre>
  
  <p>The code works, but the output XML has a lot of unnecessary white spaces. Looks like I'm doing something wrong with writing to file.</p>
  
  <p>ps. Thanks to Tim, this is how it should work (the last line of the example above shall be replaced with these three lines):</p>
  
  <pre><code>def printer = new XmlNodePrinter(new PrintWriter(writer))
  printer.preserveWhitespace = true
  printer.print(xml)
  </code></pre>