Java XML Child Remove removeChildrenOfType(final Node parent, final String type)

Here you can find the source of removeChildrenOfType(final Node parent, final String type)

Description

Removes any child elements that match the type

License

Open Source License

Parameter

Parameter Description
parent The element whose children will be removed
type The name of the children elements to remove

Declaration

public static void removeChildrenOfType(final Node parent, final String type) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;

import java.util.List;

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

public class Main {
    /**/*w w  w.  j a v a2  s .com*/
     * Removes any child elements that match the type
     * @param parent The element whose children will be removed
     * @param type The name of the children elements to remove
     */
    public static void removeChildrenOfType(final Node parent, final String type) {
        final NodeList children = parent.getChildNodes();
        for (int childIndex = 0; childIndex < children.getLength(); ++childIndex) {
            final Node child = children.item(childIndex);
            if (child.getNodeName().equals(type)) {
                parent.removeChild(child);
                break;
            }
        }
    }

    /**
     * Scans a node and all of its children for nodes of a particular type.
     *
     * @param parent    The parent node to search from.
     * @param nodeNames A single node name or list of node names to search for
     * @return A List of all the nodes found matching the nodeName(s) under the parent
     */
    public static List<Node> getChildNodes(final Node parent, final String... nodeNames) {
        return getChildNodes(parent, true, nodeNames);
    }

    /**
     * Scans a node and all of its children for nodes of a particular type.
     *
     * @param parent          The parent node to search from.
     * @param recursiveSearch If the child nodes should be recursively searched.
     * @param nodeNames       A single node name or list of node names to search for
     * @return a List of all the nodes found matching the nodeName under the parent
     */
    protected static List<Node> getChildNodes(final Node parent, boolean recursiveSearch,
            final String... nodeNames) {
        final List<Node> nodes = new ArrayList<Node>();
        final NodeList children = parent.getChildNodes();
        for (int i = 0; i < children.getLength(); ++i) {
            final Node child = children.item(i);

            for (final String nodeName : nodeNames) {
                if (child.getNodeName().equals(nodeName)) {
                    nodes.add(child);
                }
                if (recursiveSearch) {
                    nodes.addAll(getChildNodes(child, true, nodeName));
                }
            }
        }
        return nodes;
    }
}

Related

  1. removeChildren(Node parent)
  2. removeChildren(Node parent, NodeList nodelist)
  3. removeChildren(Node target)
  4. removeChildren(Node xmlNode, String name, boolean ignoreCase)
  5. removeChildrenNode(Node node)
  6. removeChilds(Node node)
  7. removeNode(Node parent, Node child)