Java XML Node Remove removeEmptyTextNodes(Node parentNode)

Here you can find the source of removeEmptyTextNodes(Node parentNode)

Description

remove Empty Text Nodes

License

LGPL

Declaration

private static void removeEmptyTextNodes(Node parentNode) 

Method Source Code

//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;
        }
    }
}

Related

  1. removeEmptyHeadings(Node root)
  2. removeEmptyHeadings(Node root)
  3. removeEmptyLines(Node node)
  4. removeEmptyNodes(Node node)
  5. removeEmptyParagraphs(Node root)
  6. removeEmptyTextNodes(Node parentNode)
  7. removeJavascripts(Node node)
  8. removeLeafNode(Node node)
  9. removeNamedItem(final NamedNodeMap nodeMap, final String name)