Java XML Child Node Get getChildNodes(Node parentNode)

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

Description

This method will get the child nodes of a given node and return them in an ArrayList instead of a NodeList.

License

Open Source License

Parameter

Parameter Description
parentNode a parameter

Return

an ArrayList of childNodes if the parentNode has childNodes. If there are no children an empty ArrayList is returned.

Declaration

private static ArrayList<Node> getChildNodes(Node parentNode) 

Method Source Code


//package com.java2s;
/*/* w w  w.j  a  v  a  2  s .c  o  m*/
 * Copyright IBM Corp. 2011
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

import java.util.ArrayList;

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

public class Main {
    /**
     * This method will get the child nodes of a given node and return them in an ArrayList
     * instead of a NodeList. NodeList does not implement Iterator, which is needed to make
     * the compare code safer.
     * @param parentNode
     * @return an ArrayList of childNodes if the parentNode has childNodes. If there are no children an empty ArrayList is returned.
     */
    private static ArrayList<Node> getChildNodes(Node parentNode) {
        ArrayList<Node> childNodesArray = new ArrayList<Node>();
        if (null != parentNode) {
            NodeList childNodes = parentNode.getChildNodes();
            if (null != childNodes && childNodes.getLength() > 0) {
                for (int i = 0; i < childNodes.getLength(); i++) {
                    Node child = childNodes.item(i);
                    if (null != child) {
                        childNodesArray.add(child);
                    }
                }
            }
        }
        return childNodesArray;
    }
}

Related

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