Java XML First Child Element getFirstChild(final Element parent, final String childName)

Here you can find the source of getFirstChild(final Element parent, final String childName)

Description

Gets the first child element node with the specified tag name of a specified parent node.

License

Apache License

Parameter

Parameter Description
parent the parent node.
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.

Declaration

public static Element getFirstChild(final Element parent, final String childName) 

Method Source Code

//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;
    }
}

Related

  1. getFirstChild(Element parent, String name)
  2. getFirstChild(Element root)
  3. getFirstChild(Element root, String name)
  4. getFirstChild(Element tag, String childTagName)
  5. getFirstChild(final Element el, final String name)
  6. getFirstChild(final Element parentElem, final String childName)
  7. getFirstChild(Node node, String childName)
  8. getFirstChild(Node parent)
  9. getFirstChildByName(Element parent, String name)