Java XML Element Get by Name getElementByTagName(Element parent, String elementName)

Here you can find the source of getElementByTagName(Element parent, String elementName)

Description

Get a child element with a given tag name.

License

Open Source License

Parameter

Parameter Description
parent Parent Element
elementName Tag name of the child element to be returned.

Return

First child element which has the given tag name.

Declaration

public static final Element getElementByTagName(Element parent, String elementName) 

Method Source Code

//package com.java2s;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    /**//from  w  ww.ja v a  2 s  .  c  om
     * Get a child element with a given tag name. If multiple child elements exist with the same tag name, the first
     * child element is returned.
     * 
     * @param parent
     *            Parent Element
     * @param elementName
     *            Tag name of the child element to be returned.
     * 
     * @return First child element which has the given tag name.
     */
    public static final Element getElementByTagName(Element parent, String elementName) {
        NodeList nodeList = parent.getElementsByTagName(elementName);

        if (nodeList.getLength() > 0) {
            return (Element) nodeList.item(0);
        }

        return null;
    }
}

Related

  1. getElementByTagAndName(Document document, String tagName, String elementName)
  2. getElementByTagName(Document doc, String eleName)
  3. getElementByTagName(Document document, String tagName)
  4. getElementByTagName(Element aParent, String aName)
  5. getElementByTagName(Element parent, String elementName)
  6. getElementByTagNameWithParents(Element n, String... elementName)
  7. getElementDateValue(Document document, Element parent, String string)
  8. getElementFloatValue(Document document, Element parent, String element)
  9. getElements(Element parent, String localName)