Here you can find the source of getFirstChildElementByName(Element parent, String tagName)
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 |
public static Element getFirstChildElementByName(Element parent, String tagName)
//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; } }