Java XML First Child Element getDirectChildNodes(final Node parent, final String... nodeNames)

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

Description

Scans a node for directly related child nodes of a particular type.

License

Open Source License

Parameter

Parameter Description
parent The parent node to search from.
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

Declaration

public static List<Node> getDirectChildNodes(final Node parent, 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   ww  w .j a va2s  .  co m*/
     * Scans a node for directly related child nodes of a particular type. This method will not scan for nodes that aren't a child of the
     * parent node.
     *
     * @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> getDirectChildNodes(final Node parent, final String... nodeNames) {
        return getChildNodes(parent, false, nodeNames);
    }

    /**
     * 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. getDirectChildElementsByTag(Element node, String tag)
  2. getDirectChildElementValue(Element p_rootElement, String p_elementName)
  3. getDirectChildElementValues(Element p_rootElement, String p_elementName)
  4. getDirectChildNamedElements( Element parent, String name)
  5. getDirectChildNode(Node node, String name)
  6. getDirectChildNodes(Node node, String name)
  7. getFirstChild(Element e)
  8. getFirstChild(Element e, String nsUri, String local)
  9. getFirstChild(Element elem, String childTag)