Java XML NodeList findElement(NodeList elements, String elementName)

Here you can find the source of findElement(NodeList elements, String elementName)

Description

Find an Element of a given name in a NodeList

License

Open Source License

Parameter

Parameter Description
elements the NodeList of elements to search
elementName the name of the Element to look for

Return

the Element if found, otherwise null

Declaration

public static Element findElement(NodeList elements, String elementName) 

Method Source Code

//package com.java2s;
/*/*  ww  w.jav  a2s.c  o  m*/
 *  This file is part of "Tweety", a collection of Java libraries for
 *  logical aspects of artificial intelligence and knowledge representation.
 *
 *  Tweety is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Find an {@code Element} of a given name in a {@code NodeList}
     * @param elements the {@code NodeList} of elements to search
     * @param elementName the name of the {@code Element} to look for
     * @return the {@code Element} if found, otherwise {@code null}
     */
    public static Element findElement(NodeList elements, String elementName) {
        for (int i = 0; i < elements.getLength(); i++) {
            Node currentNode = elements.item(i);
            if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                Element currentElement = (Element) currentNode;
                if (currentNode.getNodeName().equals(elementName)) {
                    return currentElement;
                }
            }
        }

        return null;
    }
}

Related

  1. equalNodes(NodeList sourceNodeList, NodeList targetNodeList)
  2. extractElementsFromNodeList(NodeList config, String tag, boolean required)
  3. extractNodeListFromElement(Element config, String tag, boolean required)
  4. fillHashtable(NodeList list, Hashtable fillIn)
  5. fillHashtable(NodeList list, Hashtable fillIn)
  6. findElementsIgnoreCase(String name, NodeList nodes)
  7. findNode(NodeList nodeList, String nodeName)
  8. findNode(NodeList nodes, String nodeName)
  9. findNode(String id, NodeList orgs)