Java XML Node Remove removeWhitespace(Node parent)

Here you can find the source of removeWhitespace(Node parent)

Description

remove Whitespace

License

LGPL

Declaration

public static void removeWhitespace(Node parent) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static void removeWhitespace(Node parent) {
        parent.normalize();//from  w w w .j  a  v  a  2 s.com
        NodeList nodes = parent.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    removeWhitespace((Element) node);
                } else if (node.getNodeType() == Node.TEXT_NODE) {
                    String text = node.getNodeValue().trim();
                    node.setNodeValue(text);
                }
            }
        }
    }
}

Related

  1. removeTextNodes(Node parent)
  2. removeUndesiredContent(Node node)
  3. removeUserData(Node node, String key)
  4. removeValue(NamedNodeMap values, String name)
  5. removeWhitespace(Node node, boolean deep)
  6. removeWhitespaceNodes(Node e)
  7. removeWhitespaceNodes(org.w3c.dom.Element e)
  8. removeWhitespaceTextNodes(Element parent)