Java XML First Child Element getFirstChild(Element parent, String name)

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

Description

Get the first child element with the given name.

License

Apache License

Parameter

Parameter Description
parent the parent node for which to get the first child
name the name of the child element to get; may be null or "*" to indicate the first child element of any name

Return

the child, or null if none was found

Declaration

public static Element getFirstChild(Element parent, String name) 

Method Source Code

//package com.java2s;
/* $HeadURL::                                                                            $
 * $Id$/* www .j  a v a  2 s  .  c om*/
 *
 * Copyright (c) 2006-2008 by Topaz, Inc.
 * http://topazproject.org
 *
 * 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 {
    /**
     * Get the first child element with the given name.  This differs from {@link
     * org.w3c.dom.Node#getFirstChild Node.getFirstChild} in that this only returns elements.
     *
     * @param parent the parent node for which to get the first child
     * @param name   the name of the child element to get; may be null or "*" to indicate the
     *               first child element of any name
     * @return the child, or null if none was found
     */
    public static Element getFirstChild(Element parent, String name) {
        final String filter = (name != null && !name.equals("*")) ? name : null;

        for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
            if (n.getNodeType() == Node.ELEMENT_NODE && (filter == null || n.getNodeName().equals(filter)))
                return (Element) n;
        }

        return null;
    }

    /**
     * Get the next sibling element with the given name.  This differs from {@link
     * org.w3c.dom.Node#getNextSibling Node.getNextSibling} in that this only returns elements.
     *
     * @param node   the current node for which to get the next sibling
     * @param name   the name of the next sibling element to get; may be null or "*" to indicate the
     *               next sibling of any name
     * @return the sibling, or null if none was found
     */
    public static Element getNextSibling(Element node, String name) {
        final String filter = (name != null && !name.equals("*")) ? name : null;

        for (Node n = node.getNextSibling(); n != null; n = n.getNextSibling()) {
            if (n.getNodeType() == Node.ELEMENT_NODE && (filter == null || n.getNodeName().equals(filter)))
                return (Element) n;
        }

        return null;
    }
}

Related

  1. getFirstChild(Element element)
  2. getFirstChild(Element element, String child)
  3. getFirstChild(Element element, String namespaceUri, String localName)
  4. getFirstChild(Element element, String tag)
  5. getFirstChild(Element parent, String childTagName)
  6. getFirstChild(Element parent, String name)
  7. getFirstChild(Element root)
  8. getFirstChild(Element root, String name)
  9. getFirstChild(Element tag, String childTagName)