code: protected void stripWhitespaceOnlyTextNodesImpl()
          throws XPathExpressionException
      {
          XPathFactory xpathFactory = XPathFactory.newInstance();
          // XPath to find empty text nodes.
          XPathExpression xpathExp = xpathFactory.newXPath().compile(
              "//text()[normalize-space(.) = '']");
          NodeList emptyTextNodes = (NodeList) xpathExp.evaluate(
              this.getDocument(), XPathConstants.NODESET);
  
          // Remove each empty text node from document.
          for (int i = 0; i < emptyTextNodes.getLength(); i++) {
              Node emptyTextNode = emptyTextNodes.item(i);
              emptyTextNode.getParentNode().removeChild(emptyTextNode);
          }
      }
docs: Find and delete from the underlying Document any text nodes that
  contain nothing but whitespace, such as newlines and tab or space
  characters used to indent or pretty-print an XML document.
  
  Uses approach I documented on StackOverflow:
  http://stackoverflow.com/a/979606/4970
  
  @throws XPathExpressionException