Java XML Node Find findElements(Node fromnode, String name)

Here you can find the source of findElements(Node fromnode, String name)

Description

Simple DOM elements finder.

License

LGPL

Parameter

Parameter Description
fromnode node from when we start deep search
name the node name to search

Return

the list of elements matching the seeken name

Declaration

protected static List<Element> findElements(Node fromnode, String name) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.ArrayList;
import java.util.List;

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

public class Main {
    /**//from  w  w w .ja  v  a  2  s  .  c  o m
     * Simple DOM elements finder.
     *
     * @param fromnode node from when we start deep search
     * @param name the node name to search
     * @return the list of elements matching the seeken name
     */
    protected static List<Element> findElements(Node fromnode, String name) {
        NodeList nodelist = fromnode.getChildNodes();
        List<Element> list = new ArrayList<Element>();
        for (int i = 0; i < nodelist.getLength(); i++) {
            Node node = nodelist.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                if (name.equals(node.getNodeName())) {
                    list.add((Element) node);
                }
                list.addAll(findElements(node, name));
            }
        }
        return list;
    }
}

Related

  1. findElementById(Node head, String id)
  2. findElementById(Node node, String id)
  3. findElementByName(Node node, final String name)
  4. findElementByTag(Set nodes, String tag)
  5. findElementNodeByName(Node node, String name)
  6. findElementsByName(Node node, List elements, String name)
  7. findNode(Document source, Element n)
  8. findNode(Node aNode, String aName)
  9. findNode(Node node, String nodeName)