Java XML Element Get from Parent getNodes(final Node parent, final String nodeName)

Here you can find the source of getNodes(final Node parent, final String nodeName)

Description

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

License

GNU General Public License

Parameter

Parameter Description
parent The parent node
nodeName The node name to search for

Return

a List of all the nodes found matching the nodeName under the parent

Declaration

public static List<Node> getNodes(final Node parent, final String nodeName) 

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  ww  .j ava2  s.  com*/
     * Scans a node and all of its children for nodes of a particular type.
     * 
     * @param parent
     *            The parent node
     * @param nodeName
     *            The node name to search for
     * @return a List of all the nodes found matching the nodeName under the parent
     */
    public static List<Node> getNodes(final Node parent, final String nodeName) {
        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);

            if (child.getNodeName().equals(nodeName)) {
                nodes.add(child);
            } else {
                nodes.addAll(getNodes(child, nodeName));
            }
        }
        return nodes;
    }
}

Related

  1. getNodeBean(Node parent)
  2. getNodeByLocalName(final Node parent, final String name)
  3. getNodeContents(Node parentNode)
  4. getNodeContents(String nodeName, Node parentNode)
  5. getNodeList(Element parent)