Java XML NodeList getElementFromNodeList(NodeList nl)

Here you can find the source of getElementFromNodeList(NodeList nl)

Description

Selects the (first) element from a node list and returns it.

License

EUPL

Parameter

Parameter Description
nl The NodeList to get the element from.

Return

The (first) element included in the node list or null if the node list is null or empty or no element is included in the list.

Declaration

public static Element getElementFromNodeList(NodeList nl) 

Method Source Code

//package com.java2s;
/*//  w ww .  j av  a2  s .  co m
 * Copyright 2003 Federal Chancellery Austria
 * MOA-ID has been developed in a cooperation between BRZ, the Federal
 * Chancellery Austria - ICT staff unit, and Graz University of Technology.
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * http://www.osor.eu/eupl/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 *
 * This product combines work with different licenses. See the "NOTICE" text
 * file for details on the various modules and licenses.
 * The "NOTICE" text file is part of the distribution. Any derivative works
 * that you distribute must include a readable copy of the "NOTICE" text file.
 */

import org.w3c.dom.Element;

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

public class Main {
    /**
     * Selects the (first) element from a node list and returns it.
     * 
     * @param nl  The NodeList to get the element from.
     * @return    The (first) element included in the node list or <code>null</code>
     *            if the node list is <code>null</code> or empty or no element is
     *            included in the list.
     */
    public static Element getElementFromNodeList(NodeList nl) {
        if ((nl == null) || (nl.getLength() == 0)) {
            return null;
        }
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) node;
            }
        }
        return null;
    }
}

Related

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