Java XML Node Remove removeTextNodes(Node parent)

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

Description

Removes all the immediate child text nodes.

License

Educational Community License

Parameter

Parameter Description
parent the parent

Return

the element

Declaration

private static Node removeTextNodes(Node parent) 

Method Source Code

//package com.java2s;
/**//from  w  w  w.j av a 2  s  . co m
 *  This document is a part of the source code and related artifacts
 *  for CollectionSpace, an open source collections management system
 *  for museums and related institutions:

 *  http://www.collectionspace.org
 *  http://wiki.collectionspace.org

 *  Copyright 2009 University of California at Berkeley

 *  Licensed under the Educational Community License (ECL), Version 2.0.
 *  You may not use this file except in compliance with this License.

 *  You may obtain a copy of the ECL 2.0 License at

 *  https://source.collectionspace.org/collection-space/LICENSE.txt

 *  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;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Removes all the immediate child text nodes.
     *
     * @param parent
     *            the parent
     * @return the element
     */
    private static Node removeTextNodes(Node parent) {
        Node result = parent;

        NodeList nodeList = parent.getChildNodes();
        int nodeListSize = nodeList.getLength();
        for (int i = 0; i < nodeListSize; i++) {
            Node child = nodeList.item(i);
            if (child != null && child.getNodeType() == Node.TEXT_NODE) {
                parent.removeChild(child);
            }
        }

        return result;
    }
}

Related

  1. removeNodes(Element parent, NodeList elements)
  2. removeNodesByName(Node node, String name)
  3. removePreviousWhiteSpace(Node node)
  4. removeQueryCallBody(Node queryElement)
  5. removeSelf(Node node)
  6. removeUndesiredContent(Node node)
  7. removeUserData(Node node, String key)
  8. removeValue(NamedNodeMap values, String name)
  9. removeWhitespace(Node node, boolean deep)