Java XML Child Node Get getChildNodes(Node node)

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

Description

Method that returns a only the Element childs of a Node

License

Open Source License

Parameter

Parameter Description
node the Node to inspect

Return

a list of the Element childs of the node

Declaration

public static List<Element> getChildNodes(Node node) 

Method Source Code


//package com.java2s;
/*/*from ww w .  jav  a  2s. co m*/
Copyright 2012 Juan Mart?n Runge
    
jmrunge@gmail.com
http://www.zirsi.com.ar
    
This file is part of ZirUtils.
    
ZirUtils 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.
    
ZirUtils 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 ZirUtils.  If not, see <http://www.gnu.org/licenses/>.
*/

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

public class Main {
    /**
     * Method that returns a only the Element childs of a Node
     * 
     * @param node the Node to inspect
     * @return a list of the Element childs of the node
     * @see #getNodeList(NodeList)
     */
    public static List<Element> getChildNodes(Node node) {
        return getNodeList(node.getChildNodes());
    }

    /**
     * Method that returns only the Element objects of the specified NodeList
     * 
     * @param nodeList the NodeList to inspect
     * @return a List containing only the Element objects of the NodeList
     */
    public static List<Element> getNodeList(NodeList nodeList) {
        List<Element> nodes = new ArrayList<>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node instanceof Element)
                nodes.add((Element) node);
        }
        return nodes;
    }
}

Related

  1. getChildNodes(final Node node, final short nodetype)
  2. getChildNodes(final Node node, final String attributeName, final String value)
  3. getChildNodes(final Node node, short... types)
  4. getChildNodes(final Node parent, boolean recursiveSearch, final String... nodeNames)
  5. getChildNodes(Node node)
  6. getChildNodes(Node node)
  7. getChildNodes(Node node, short type)
  8. getChildNodes(Node node, String name)
  9. getChildNodes(Node parent, String childName)