Java tutorial
//package com.java2s; /* * Copyright 2009-2010 Andreas Veithen * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import org.w3c.dom.Node; public class Main { /** * Remove empty CDATA sections from the given node and all of its descendants. Some parsers * silently discard empty CDATA sections, and this method may be used to compare the output from * parsers that behave differently with respect to empty CDATA sections. * * @param node * the node to process */ public static void removeEmptyCDATASections(Node node) { Node child = node.getFirstChild(); while (child != null) { Node next = child.getNextSibling(); switch (child.getNodeType()) { case Node.CDATA_SECTION_NODE: if (child.getNodeValue().length() == 0) { child.getParentNode().removeChild(child); } break; case Node.ELEMENT_NODE: removeEmptyCDATASections(child); } child = next; } } }