Here you can find the source of getChildElement(Node parent, String childName)
Parameter | Description |
---|---|
parent | Parent node |
childName | Name of the child node or null for the first child |
public static Element getChildElement(Node parent, String childName)
//package com.java2s; /*//from w w w.j a v a 2s . c om * Copyright 2007 skynamics AG * * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0 * * 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; public class Main { /** * Gets a named child node. * In contrast to the Element.getElementsByTagName method, this method is fine * if there is only a single element by that name.<br> * Note: Only child \belements\b will be considered. * * @param parent Parent node * @param childName Name of the child node or null for the first child * @return The child node or null if no such node exists */ public static Element getChildElement(Node parent, String childName) { return (Element) getChildNode(parent, childName, Node.ELEMENT_NODE); } /** * Gets a named child node. * In contrast to the Element.getElementsByTagName method, this method is fine * if there is only a single element by that name.<br> * Note: Only child \belements\b will be considered. * * @deprecated {@link #getChildElement} * @param parent Parent node * @param childName Name of the child node or null for the first child * @return The child node or null if no such node exists */ public static Node getChildNode(Node parent, String childName) { return getChildElement(parent, childName); } /** * Gets a named child node by the node type. * In contrast to the Element.getElementsByTagName * method, this method is fine if there is only a single element by that name.\n Note: Only * child node of the given node type will be considered. * * @param parent Parent node * @param childName Name of the child node or null for the first child * @param nodeType see types of interface Node * @return The child node or null if no such node exists */ public static Node getChildNode(Node parent, String childName, short nodeType) { // Search the child nodes for the correct one for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() != nodeType) continue; if (childName == null || childName.equals(child.getNodeName())) { return child; } } return null; } }