Java tutorial
//package com.java2s; /* // This software is subject to the terms of the Eclipse Public License v1.0 // Agreement, available at the following URL: // http://www.eclipse.org/legal/epl-v10.html. // You must accept the terms of that agreement to use this software. // // Copyright (C) 2004-2005 Julian Hyde // Copyright (C) 2005-2009 Pentaho and others // All Rights Reserved. */ import org.w3c.dom.*; import java.util.regex.Pattern; public class Main { static final Pattern WhitespacePattern = Pattern.compile("\\s*"); public static void stripWhitespace(Element element) { final NodeList childNodeList = element.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { Node node = childNodeList.item(i); switch (node.getNodeType()) { case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: final String text = ((CharacterData) node).getData(); if (WhitespacePattern.matcher(text).matches()) { element.removeChild(node); --i; } break; case Node.ELEMENT_NODE: stripWhitespace((Element) node); break; } } } }