Java XML Child Remove removeChildNodes(Node node, short... ignoreNodeTypes)

Here you can find the source of removeChildNodes(Node node, short... ignoreNodeTypes)

Description

remove Child Nodes

License

Open Source License

Parameter

Parameter Description
node a parameter

Declaration

public static void removeChildNodes(Node node, short... ignoreNodeTypes) 

Method Source Code

//package com.java2s;

import java.util.HashSet;
import java.util.LinkedHashSet;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**//from  w ww  .  j a v a2 s .  c om
     * @param node
     */
    public static void removeChildNodes(Node node, short... ignoreNodeTypes) {
        HashSet<Node> childNodesSet = getValidChildNodes(node, false, ignoreNodeTypes);
        for (Node childNode : childNodesSet) {
            node.removeChild(childNode);
        }
    }

    /**
     * Returns only the valid child nodes of a node. Eliminates the nodes from
     * the list of child nodes of the input node if the child node type is same
     * with any of <code>ignoreNodeTypes</code>
     * 
     * @param node
     * @param maintainOrder
     * @param ignoreNodeTypes
     * @return HashSet<Node>
     */
    public static HashSet<Node> getValidChildNodes(Node node, boolean maintainOrder, short... ignoreNodeTypes) {
        NodeList childNodes = node.getChildNodes();
        HashSet<Node> filteredChildNodes = new HashSet<Node>();
        if (maintainOrder) {
            filteredChildNodes = new LinkedHashSet<Node>();
        }
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node thisNode = childNodes.item(i);
            boolean allowAdd = true;
            for (int j = 0; j < ignoreNodeTypes.length; j++) {
                if (ignoreNodeTypes[j] == thisNode.getNodeType()) {
                    allowAdd = false;
                    break;
                }
            }
            if (allowAdd) {
                filteredChildNodes.add(thisNode);
            }
        }
        return filteredChildNodes;
    }
}

Related

  1. removeChild(Node xmlNode, String name, boolean ignoreCase)
  2. removeChild(NodeList nodes)
  3. removeChildElements(Element parent)
  4. removeChildNodes(Node node)
  5. removeChildNodes(Node node)
  6. removeChildNodes(Node parent)
  7. removeChildren(Element el)
  8. removeChildren(Element element)
  9. removeChildren(Element element)