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

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

Description

Returns all child nodes of the given root node with the given name.

License

Open Source License

Parameter

Parameter Description
rootNode The root node
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

Declaration

public static Node[] getChildNodes(Node rootNode, String childName) 

Method Source Code


//package com.java2s;
/*//from w  ww  .jav  a 2s.c om
 * 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 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. getChildNodes(Node node, String name)
  2. getChildNodes(Node parent, String childName)
  3. getChildNodes(Node parentNode)
  4. getChildNodes(Node parentNode, String childName)
  5. getChildNodes(Node parentNode, String childNodeName)
  6. getChildNodesByName(Element parent, String name)
  7. getChildNodesByName(Node element, String name, boolean caseSensitive)
  8. getChildNodesByName(Node element, String name, boolean caseSensitive)
  9. getChildNodesByName(Node element, String name, boolean caseSensitive)