Here you can find the source of getFirstLevelChildElementsByTagName(Element parent, String elementName)
Parameter | Description |
---|---|
parent | Parent Element |
elementName | List of all the first level child Elements with a given tag name. |
public static final List<Element> getFirstLevelChildElementsByTagName(Element parent, String elementName)
//package com.java2s; import java.util.ArrayList; import java.util.List; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /**// w w w . j a va2 s.co m * Get all the first level child elements with a given tag name. * * @param parent * Parent Element * @param elementName * List of all the first level child Elements with a given tag name. * * @return List of all the first level child elements with a given tag name. */ public static final List<Element> getFirstLevelChildElementsByTagName(Element parent, String elementName) { NodeList nodeList = parent.getChildNodes(); ArrayList<Element> childList = new ArrayList<Element>(nodeList.getLength()); Node childNode = parent.getFirstChild(); while (childNode != null) { if (childNode.getNodeType() == Node.ELEMENT_NODE) { String localName = ((Element) childNode).getLocalName(); if (localName.equals(elementName)) { childList.add((Element) childNode); } } childNode = childNode.getNextSibling(); } return childList; } }