Here you can find the source of getChildElement(Element element, String elementName)
Parameter | Description |
---|---|
element | a parameter |
elementName | a parameter |
public static Element getChildElement(Element element, String elementName)
//package com.java2s; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /**/*from w w w.ja va 2s . c o m*/ * Returns the first child element with the specified name, or null if none exists. * @param element * @param elementName * @return Element */ public static Element getChildElement(Element element, String elementName) { for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) { if (node instanceof Element) { Element childElem = (Element) node; String elemName = getElementName(childElem); if (elementName.equals(elemName)) return childElem; } } return null; } /** * Returns the element name of the given element. * @param element * @return String */ public static String getElementName(Element element) { // When loading from disk the local name will be setup correctly, but if the local name // is fetched directly after calling Document.createElement() then the value will be null. // See the JavaDoc for more info. To workaround this, use the complete node name. String elemName = element.getLocalName(); if (elemName == null) { elemName = element.getNodeName(); } return elemName; } }