title: How to strip whitespace-only text nodes from a DOM before serialization?
body: <p>I have some Java (5.0) code that constructs a DOM from various (cached) data sources, then removes certain element nodes that are not required, then serializes the result into an XML string using:</p>
  
  <pre><code>// Serialize DOM back into a string
  Writer out = new StringWriter();
  Transformer tf = TransformerFactory.newInstance().newTransformer();
  tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  tf.setOutputProperty(OutputKeys.INDENT, "no");
  tf.transform(new DOMSource(doc), new StreamResult(out));
  return out.toString();
  </code></pre>
  
  <p>However, since I'm removing several element nodes, I end up with a lot of extra whitespace in the final serialized document.</p>
  
  <p>Is there a simple way to remove/collapse the extraneous whitespace from the DOM before (or while) it's serialized into a String?</p>