Java tutorial
//package com.java2s; import java.util.*; import org.w3c.dom.*; public class Main { public static void cleanWhiteSpaceNodes(Element node, boolean deep) { NodeList list = node.getChildNodes(); ArrayList temp = new ArrayList(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); short type = n.getNodeType(); if (type == 1) { Element e = (Element) n; cleanWhiteSpaceNodes(e, deep); } else if (type == 3) { Text text = (Text) n; String val = text.getData(); if (val.trim().equals("")) temp.add(text); } } for (Iterator i = temp.iterator(); i.hasNext(); node.removeChild((Node) i.next())) ; } }