Java XML Child Node Get getChildNode(Node rootNode, String childName)

Here you can find the source of getChildNode(Node rootNode, String childName)

Description

Returns the first child node of the given root node that has the given name.

License

Open Source License

Parameter

Parameter Description
rootNode The root node
childName The name of the child node

Return

The child node, or null if the root node does not have a child node with the given name

Declaration

public static Node getChildNode(Node rootNode, String childName) 

Method Source Code


//package com.java2s;
/*// w w w.java2 s  .  c o  m
 * utils - DOMUtil.java - Copyright ? 2006-2009 David Roden
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Returns the first child node of the given root node that has the given
     * name.
     *
     * @param rootNode
     *            The root node
     * @param childName
     *            The name of the child node
     * @return The child node, or {@code null} if the root node does not have a
     *         child node with the given name
     */
    public static Node getChildNode(Node rootNode, String childName) {
        NodeList nodeList = rootNode.getChildNodes();
        for (int nodeIndex = 0, nodeSize = nodeList.getLength(); nodeIndex < nodeSize; nodeIndex++) {
            Node childNode = nodeList.item(nodeIndex);
            if (childNode.getNodeName().equals(childName)) {
                return childNode;
            }
        }
        return null;
    }

    /**
     * Returns all child nodes of the given root node with the given name.
     *
     * @param rootNode
     *            The root node
     * @param childName
     *            The name of child nodes
     * @return All child nodes with the given name, or an empty array if there
     *         are no child nodes with the given name
     */
    public static Node[] getChildNodes(Node rootNode, String childName) {
        List<Node> nodes = new ArrayList<Node>();
        NodeList nodeList = rootNode.getChildNodes();
        for (int nodeIndex = 0, nodeSize = nodeList.getLength(); nodeIndex < nodeSize; nodeIndex++) {
            Node childNode = nodeList.item(nodeIndex);
            if (childNode.getNodeName().equals(childName)) {
                nodes.add(childNode);
            }
        }
        return nodes.toArray(new Node[nodes.size()]);
    }
}

Related

  1. getChildNode(Node node, String tag)
  2. getChildNode(Node parent, String childName)
  3. getChildNode(Node parent, String tagName)
  4. getChildNode(Node parentNode, String childElementName)
  5. getChildNode(Node root, String childName)
  6. getChildNodeByLocalName(final Node parentNode, final String localNodeName)
  7. getChildNodeByName(final Node xnode, final String name, final boolean caseSensitive)
  8. getChildNodeByName(Node element, CharSequence nodeName, boolean caseSensitive)
  9. getChildNodeByName(Node node, String childName)