Java XML First Child Element getFirstChildWithTagName(Node data, String tagName)

Here you can find the source of getFirstChildWithTagName(Node data, String tagName)

Description

The function getFirstChildWithTagName() returns the first child node from a node, identified by its node name.

License

Open Source License

Parameter

Parameter Description
data Document or Element whose children shall be examined
tagName name of the node to find

Exception

Parameter Description
NoSuchElementException if no child node with that name can be found

Return

first child node with that node name

Declaration

public static Element getFirstChildWithTagName(Node data, String tagName) throws NoSuchElementException 

Method Source Code

//package com.java2s;
/*/*  w  ww. ja v a  2 s  .c  o m*/
 * (c) Kitodo. Key to digital objects e. V. <contact@kitodo.org>
 *
 * This file is part of the Kitodo project.
 *
 * It is licensed under GNU General Public License version 3 or later.
 *
 * For the full copyright and license information, please read the
 * GPL3-License.txt file that was distributed with this source code.
 */

import java.util.NoSuchElementException;

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

public class Main {
    /**
     * The function getFirstChildWithTagName() returns the first child node from
     * a node, identified by its node name.
     *
     * @param data
     *            Document or Element whose children shall be examined
     * @param tagName
     *            name of the node to find
     * @return first child node with that node name
     * @throws NoSuchElementException
     *             if no child node with that name can be found
     */
    public static Element getFirstChildWithTagName(Node data, String tagName) throws NoSuchElementException {
        for (Node element = data.getFirstChild(); element != null; element = element.getNextSibling()) {
            if (!(element instanceof Element)) {
                continue;
            }
            if (element.getNodeName().equals(tagName)) {
                return (Element) element;
            }
        }
        throw new NoSuchElementException(tagName);
    }
}

Related

  1. getFirstChildTextByTagName(Element element, String tagName)
  2. getFirstChildTextContent(Node node)
  3. getFirstChildTextNodeValue(Node node)
  4. getFirstChildTextValue(Element parent, String childName)
  5. getFirstChildWithTagName(Element parent, String tagName)
  6. getFirstElement(Element element, String childName)
  7. getFirstElementChild(Element aElement)
  8. getFirstElementChild(Element element)
  9. getFirstElementChild(Element elemNode)