Here you can find the source of getFirstChild(final Element parent, final String childName)
Parameter | Description |
---|---|
parent | the parent node. |
childName | the specified tag name. |
public static Element getFirstChild(final Element parent, final String childName)
//package com.java2s; /*// w w w .j a v a2s . c o m * Copyright (c) 2014 Haixing Hu * * 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; import org.w3c.dom.NodeList; public class Main { /** * Gets the first child element node with the specified tag name of a * specified parent node. * <p> * Note that this function does NOT check the number of children nodes with * the specified child name, instead, it just find the first child node with * the specified child name and return it. * * @param parent * the parent node. * @param childName * the specified tag name. * @return the first child element node with the specified tag name of a * specified parent node; returns null if the parent has no child * element node with the specified tag name. * @see getOptionalChild * @see getRequiredChild */ public static Element getFirstChild(final Element parent, final String childName) { final NodeList childrenList = parent.getChildNodes(); final int childrenCount = childrenList.getLength(); for (int i = 0; i < childrenCount; ++i) { final Node child = childrenList.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { final Element element = (Element) child; if (childName.equals(element.getTagName())) { return element; } } } return null; } }