Java XML First Child Element getFirstChildElementByName(Node node, String name)

Here you can find the source of getFirstChildElementByName(Node node, String name)

Description

Get the first child Element of the supplied node that matches a given tag name.

License

Open Source License

Parameter

Parameter Description
node The DOM Node.
name The name of the child node to search for.

Return

The first child element with the matching tag name.

Declaration

public static Element getFirstChildElementByName(Node node, String name) 

Method Source Code

//package com.java2s;
/*// w  w  w . j a  v a  2 s .co m
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Get the first child Element of the supplied node that matches a given tag name.
     *
     * @param node The DOM Node.
     * @param name The name of the child node to search for.
     * @return The first child element with the matching tag name.
     */
    public static Element getFirstChildElementByName(Node node, String name) {
        NodeList children = node.getChildNodes();
        int childCount = children.getLength();

        for (int i = 0; i < childCount; i++) {
            Node child = children.item(i);
            if (child != null && child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName() != null
                    && child.getNodeName().equals(name)) {
                return (Element) child;
            }
        }

        return null;
    }
}

Related

  1. getFirstChildElement(Node start)
  2. getFirstChildElementByLocalName(Element parentElem, String localName)
  3. getFirstChildElementByName(Element parent, String tagName)
  4. getFirstChildElementByName(Element root, String tagName)
  5. getFirstChildElementByName(final Element parent, final String name)
  6. getFirstChildElementByTagName(Element element, String name)
  7. getFirstChildElementByTagName(Node element, String tagName)
  8. getFirstChildElementCaseInsensitive(Element root, String elementName)
  9. getFirstChildElementInternal(final Node node, final boolean useNamespaces, final String namespaceURI, final String localName)