Returns the first XML Element node in a NodeList - Android XML

Android examples for XML:XML Node

Description

Returns the first XML Element node in a NodeList

Demo Code


//package com.java2s;
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  om
     * Returns the first Element node in a NodeList
     * @param list the list to search
     * @return the element or null if none is found
     */
    public static Element getFirstElement(NodeList list) {
        if (list.getLength() == 0) {
            return null;
        }

        Node node = list.item(0);
        while (node.getNodeType() != Node.ELEMENT_NODE) {
            if (node.getNextSibling() == null) {
                return null;
            }
            node = node.getNextSibling();
        }
        return (Element) node;
    }
}

Related Tutorials