Java XML First Child Element getFirstChildElementByTagName(Node element, String tagName)

Here you can find the source of getFirstChildElementByTagName(Node element, String tagName)

Description

Gets the first element with the given tag name in the immediate child elements.

License

Open Source License

Parameter

Parameter Description
element a parameter
tagName a parameter

Declaration

public static Element getFirstChildElementByTagName(Node element, String tagName) 

Method Source Code

//package com.java2s;
// are made available under the terms of the Eclipse Public License v1.0

import org.w3c.dom.Element;

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

public class Main {
    /**//  w  w  w. ja  va2s  .  c  om
     * Gets the first element with the given tag name in the immediate child elements.
     * 
     * @param element
     * @param tagName
     * @return
     */
    public static Element getFirstChildElementByTagName(Node element, String tagName) {
        NodeList list = element.getChildNodes();
        int size = list.getLength();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                Node node = list.item(i);
                if (node instanceof Element) {
                    Element e = (Element) node;
                    if (e.getTagName().equals(tagName)) {
                        return e;
                    }
                }
            }
        }
        return null;
    }
}

Related

  1. getFirstChildElementByName(Element parent, String tagName)
  2. getFirstChildElementByName(Element root, String tagName)
  3. getFirstChildElementByName(final Element parent, final String name)
  4. getFirstChildElementByName(Node node, String name)
  5. getFirstChildElementByTagName(Element element, String name)
  6. getFirstChildElementCaseInsensitive(Element root, String elementName)
  7. getFirstChildElementInternal(final Node node, final boolean useNamespaces, final String namespaceURI, final String localName)
  8. getFirstChildElementNode(Node node)
  9. getFirstChildElementNS(Element elm, String tns, String localName)