Here you can find the source of getChildElements(Element ele, String childEleName)
Parameter | Description |
---|---|
ele | the DOM element to analyze |
childEleName | the child element getName to look for |
org.w3c.dom.Element
instances
public static List<Node> getChildElements(Element ele, String childEleName)
//package com.java2s; //License from project: Open Source License 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 . ja v a 2s .c o m * Retrieve all child elements of the given DOM element that match * the given element getName. Only look at the direct child level of the * given element; do not go into further depth (in contrast to the * DOM API's <code>getElementsByTagName</code> method). * @param ele the DOM element to analyze * @param childEleName the child element getName to look for * @return a List of child <code>org.w3c.dom.Element</code> instances * @see org.w3c.dom.Element * @see org.w3c.dom.Element#getElementsByTagName */ public static List<Node> getChildElements(Element ele, String childEleName) { NodeList nl = ele.getChildNodes(); List<Node> childEles = new ArrayList<Node>(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element && nodeNameEquals(node, childEleName)) childEles.add(node); } return childEles; } /** * Namespace-aware equals comparison. Returns <code>true</code> if either * {@link Node#getLocalName} or {@link Node#getNodeName} equals <code>desiredName</code>, * otherwise returns <code>false</code>. */ public static boolean nodeNameEquals(Node node, String desiredName) { assert node != null : "Node must not be null"; assert desiredName != null : "Desired getName must not be null"; return desiredName.equals(node.getNodeName()) || desiredName.equals(node.getLocalName()); } }