Java XML Child Get by Name getChildElement(final Node parentNode, final String childNodeName)

Here you can find the source of getChildElement(final Node parentNode, final String childNodeName)

Description

Get the first child element with the specified name from the given parent node.

License

Open Source License

Parameter

Parameter Description
parentNode node to retrieve a single child element from
childNodeName name of the targeted child element

Return

the first child element with the given name

Declaration

public static Element getChildElement(final Node parentNode, final String childNodeName) 

Method Source Code

//package com.java2s;
/*//from w  w  w .  j a v  a2  s. com
   Copyright (C) 2016 HermeneutiX.org
    
   This file is part of SciToS.
    
   SciToS is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
    
   SciToS 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 General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with SciToS. If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Get the first child element with the specified name from the given parent node. Returns {@code null} if no child element with the specified
     * name exists.
     *
     * @param parentNode
     *            node to retrieve a single child element from
     * @param childNodeName
     *            name of the targeted child element
     * @return the first child element with the given name
     */
    public static Element getChildElement(final Node parentNode, final String childNodeName) {
        final NodeList candidates = parentNode.getChildNodes();
        final int childCount = candidates.getLength();
        for (int childIndex = 0; childIndex < childCount; childIndex++) {
            final Node singleChild = candidates.item(childIndex);
            if (singleChild instanceof Element && childNodeName.equals(((Element) singleChild).getTagName())) {
                // found a matching child element, ignore potentially following matches
                return (Element) singleChild;
            }
        }
        return null;
    }
}

Related

  1. getChildElement(Element root, String name, boolean mandatory)
  2. getChildElement(final Element element, final String namespace, final String tagName)
  3. getChildElement(final Element parent, final String childName)
  4. getChildElement(final Element parent, final String ns, final String localName)
  5. getChildElement(final Node parent, final String name)
  6. getChildElement(Node node)
  7. getChildElement(Node node, String childName)
  8. getChildElement(Node parent, String childLocalName, String childNamespaceURI)
  9. getChildElement(Node parent, String childName)