Here you can find the source of removeEmptyTextNodes(Node parentNode)
private static void removeEmptyTextNodes(Node parentNode)
//package com.java2s; //License from project: LGPL import org.w3c.dom.Node; public class Main { private static void removeEmptyTextNodes(Node parentNode) { Node childNode = parentNode.getFirstChild(); while (childNode != null) { // grab the "nextSibling" before the child node is removed Node nextChild = childNode.getNextSibling(); short nodeType = childNode.getNodeType(); if (nodeType == Node.TEXT_NODE) { boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty(); if (containsOnlyWhitespace) { parentNode.removeChild(childNode); }/*from w w w . j a v a 2 s .c om*/ } childNode = nextChild; } } }