Java XML CData removeEmptyCDATASections(Node node)

Here you can find the source of removeEmptyCDATASections(Node node)

Description

Remove empty CDATA sections from the given node and all of its descendants.

License

Apache License

Parameter

Parameter Description
node the node to process

Declaration

public static void removeEmptyCDATASections(Node node) 

Method Source Code

//package com.java2s;
/*/* w w w  . jav a  2s  .  co m*/
 * 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;
        }
    }
}

Related

  1. getCharacterDataFromElement(Element e)
  2. getCharacterDataFromElement(Element e)
  3. getCharacterDataFromElementWithKey(final Element element, final String key)
  4. isCDataOrText(int nodeType)
  5. parseCdataSection(Node cdataParentNode)
  6. setTextValue(Node node, String value, boolean cdata)
  7. writeRcData(Node child, Writer out)
  8. writeXmlElementCData(XMLStreamWriter out, String name, String value)