Java XML First Child Element getFirstChildElementByName(Element parent, String tagName)

Here you can find the source of getFirstChildElementByName(Element parent, String tagName)

Description

Return the first child element for the given tag name

License

Apache License

Parameter

Parameter Description
parent The element from which the children elements must be retrieved.
tagName The tag name of the children element that must be retrieved

Return

One child element. If no elements of the specified tag name are found, null is returned.

Declaration

public static Element getFirstChildElementByName(Element parent, String tagName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.LinkedList;
import java.util.List;

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

public class Main {
    /**/*from  w  ww. j  ava  2 s  .co m*/
     * Return the first child element for the given tag name
     * 
     * @param parent
     *            The element from which the children elements must be
     *            retrieved.
     * @param tagName
     *            The tag name of the children element that must be retrieved
     * @return One child element. If no elements of the specified tag name are
     *         found, null is returned.
     */
    public static Element getFirstChildElementByName(Element parent, String tagName) {
        List<Element> elementList = getChildElementsByName(parent, tagName);
        if (!elementList.isEmpty())
            return elementList.get(0);
        else
            return null;
    }

    /**
     * Get all child elements of a certain tag name.
     * 
     * @param parent
     *            The element from which the children elements must be
     *            retrieved.
     * @param tagName
     *            The tag name of the children elements that must be retrieved
     * @return A list of children elements. If no elements of the specified tag
     *         name are found, an empty list is returned.
     */
    public static List<Element> getChildElementsByName(Element parent, String tagName) {
        List<Element> elementList = new LinkedList<Element>();
        NodeList children = parent.getChildNodes();
        for (int c = 0; c < children.getLength(); c++) {
            if (children.item(c).getNodeType() == Node.ELEMENT_NODE
                    && children.item(c).getNodeName().equals(tagName)) {
                elementList.add((Element) children.item(c));
            }
        }
        return elementList;
    }
}

Related

  1. getFirstChildElement(Node parent)
  2. getFirstChildElement(Node parent)
  3. getFirstChildElement(Node rootNode)
  4. getFirstChildElement(Node start)
  5. getFirstChildElementByLocalName(Element parentElem, String localName)
  6. getFirstChildElementByName(Element root, String tagName)
  7. getFirstChildElementByName(final Element parent, final String name)
  8. getFirstChildElementByName(Node node, String name)
  9. getFirstChildElementByTagName(Element element, String name)