code: private static void stripEmptyTextNodes(Node n) {
      NodeList children = n.getChildNodes();
      int length = children.getLength();
      for (int i = 0; i < length; i++) {
        Node c = children.item(i);
        if (!c.hasChildNodes() && c.getNodeType() == Node.TEXT_NODE &&
          c.getTextContent().trim().length() == 0) {
          n.removeChild(c);
          i--;
          length--;
          children = n.getChildNodes();
        } else {
          stripEmptyTextNodes(c);
        }
      }
    }
docs: Hack...since DOM reads newlines as textnodes we want to strip out those
  nodes to make it easier to use the tree.