Java XML Child Node Get getChildNodes(final Node parent, boolean recursiveSearch, final String... nodeNames)

Here you can find the source of getChildNodes(final Node parent, boolean recursiveSearch, final String... nodeNames)

Description

Scans a node and all of its children for nodes of a particular type.

License

Open Source License

Parameter

Parameter Description
parent The parent node to search from.
recursiveSearch If the child nodes should be recursively searched.
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

Declaration

protected static List<Node> getChildNodes(final Node parent, boolean recursiveSearch,
        final String... nodeNames) 

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 {
    /**/*from w  w  w .j  a v  a  2 s  .  c  o  m*/
     * 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. getChildNodes(Element ele)
  2. getChildNodes(final Node node)
  3. getChildNodes(final Node node, final short nodetype)
  4. getChildNodes(final Node node, final String attributeName, final String value)
  5. getChildNodes(final Node node, short... types)
  6. getChildNodes(Node node)
  7. getChildNodes(Node node)
  8. getChildNodes(Node node)
  9. getChildNodes(Node node, short type)