code: static void removeWhitespace(Node node)
      {
          NodeList childList = node.getChildNodes();
          List<Node> toRemove = new ArrayList<Node>();
          for (int i = 0; i < childList.getLength(); i++)
          {
              Node child = childList.item(i);
              if (child.getNodeType() == Node.TEXT_NODE)
              {
                  String text = child.getTextContent();
                  String trimmed = text.trim();
                  if (trimmed.isEmpty())
                  {
                      toRemove.add(child);
                  }
                  else if (trimmed.length() < text.length())
                  {
                      child.setTextContent(trimmed);
                  }
              }
              removeWhitespace(child);
          }
          for (Node c : toRemove)
          {
              node.removeChild(c);
          }
      }
docs: Removes any whitespace from the text nodes that are descendants
  of the given node. Any text node that becomes empty due to this
  (that is, any text node that only contained whitespaces) will
  be removed.
  
  @param node The node to remove whitespaces from