Java XML NodeList getElementList(NodeList list)

Here you can find the source of getElementList(NodeList list)

Description

Filters all elements of a NodeList and returns them in a collection.

License

LGPL

Parameter

Parameter Description
list Nodelist containing all types of nodes (text nodes etc.)

Return

a list containing all elements from nodeist, but not containing other nodes such as text nodes etc.

Declaration

public static List<Element> getElementList(NodeList list) 

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  ww.  ja v  a 2  s .  c om*/
     * Filters all elements of a NodeList and returns them in a collection.
     *
     * @param list Nodelist containing all types of nodes (text nodes etc.)
     * @return a list containing all elements from nodeist, but not containing other nodes such as text nodes etc.
     */
    public static List<Element> getElementList(NodeList list) {
        List<Element> col = new ArrayList<>(list.getLength());
        for (int i = 0; i < list.getLength(); i++) {
            if (list.item(i) instanceof Element) {
                col.add((Element) list.item(i));
            }
        }
        return col;
    }

    /**
     * Filters all elements of a NodeList and returns them in a collection. The list will only contain that elements of
     * the NodeList that match the specified node name. The name selection is case insensitive.
     *
     * @param list      Nodelist containing all types of nodes (text nodes etc.)
     * @param nodeNames the name of the elements to be selected (case insensitive)
     * @return a list containing all elements from nodelist, but not containing other nodes such as text nodes etc.
     */
    public static List<Element> getElementList(NodeList list, String... nodeNames) {
        List<Element> col = new ArrayList<>();
        for (int i = 0; i < list.getLength(); i++) {
            Node item = list.item(i);
            if (item instanceof Element) {
                for (String nodeName : nodeNames) {
                    if (item.getNodeName().equalsIgnoreCase(nodeName)) {
                        col.add((Element) item);
                        break;
                    }
                }
            }
        }

        return col;
    }
}

Related

  1. getDeepNode(String name[], NodeList nodes)
  2. getDSNameListInScriptText(String scriptText, NodeList tableList)
  3. getElementById(NodeList nodeList, String id)
  4. getElementFromItem(NodeList list, int index)
  5. getElementFromNodeList(NodeList nl)
  6. getElements(NodeList nodeList, String localname, String namespaceURI)
  7. getElementsFromNodeList(final NodeList nodeList)
  8. getElementsOfNodeList(NodeList nodeList)
  9. getEnvVariableMap(NodeList nodes)