Java XML Child Node Get getChildNodes(Node parentNode, String childNodeName)

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

Description

Returns an array of all child nodes of parentNode with the given node name or an empty list if no such child nodes exist.

License

Open Source License

Parameter

Parameter Description
parentNode a node
childNodeName node name of child nodes, may be <code>null</code>

Return

a list of nodes

Declaration

public static Node[] getChildNodes(Node parentNode, String childNodeName) 

Method Source Code

//package com.java2s;
/*//from w w w . jav  a  2 s  . c  o m
 * The MIT License (MIT)
 * 
 * Copyright (c) 2010 Technische Universitaet Berlin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.Vector;

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

public class Main {
    /**
     * Returns an array of all child nodes of <code>parentNode</code> with the given node name or
     * an empty list if no such child nodes exist. If the node name is <code>null</code> every
     * non-text and non-comment node will be returned.
     * 
     * @param parentNode a node
     * @param childNodeName node name of child nodes, may be <code>null</code>
     * @return a list of nodes
     */
    public static Node[] getChildNodes(Node parentNode, String childNodeName) {
        Vector children = new Vector();
        NodeList nodes = parentNode.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node n = nodes.item(i);
            if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.COMMENT_NODE)
                continue;
            if (childNodeName == null || n.getNodeName().equals(childNodeName))
                children.add(n);
        }
        Node[] result = new Node[children.size()];
        children.toArray(result);
        return result;
    }
}

Related

  1. getChildNodes(Node node, short type)
  2. getChildNodes(Node node, String name)
  3. getChildNodes(Node parent, String childName)
  4. getChildNodes(Node parentNode)
  5. getChildNodes(Node parentNode, String childName)
  6. getChildNodes(Node rootNode, String childName)
  7. getChildNodesByName(Element parent, String name)
  8. getChildNodesByName(Node element, String name, boolean caseSensitive)
  9. getChildNodesByName(Node element, String name, boolean caseSensitive)