Here you can find the source of getChildElement(Node parent, String name)
Parameter | Description |
---|---|
parent | the parent node |
name | the name of the child node |
public static Element getChildElement(Node parent, String name)
//package com.java2s; /**//w w w. j a va 2s . c om * Copyright 2005-2015 The Kuali Foundation * * Licensed under the Educational Community License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.opensource.org/licenses/ecl2.php * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Returns a node child with the specified tag name of the specified parent node, * or null if no such child node is found. * @param parent the parent node * @param name the name of the child node * @return child node if found, null otherwise */ public static Element getChildElement(Node parent, String name) { NodeList childList = parent.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { Node node = childList.item(i); // we must test against NodeName, not just LocalName // LocalName seems to be null - I am guessing this is because // the DocumentBuilderFactory is not "namespace aware" // although I would have expected LocalName to default to // NodeName if (node.getNodeType() == Node.ELEMENT_NODE && (name.equals(node.getLocalName()) || name.equals(node.getNodeName()))) { return (Element) node; } } return null; } }