Get the first child node that is XML element node. - Java XML

Java examples for XML:XML Node Child

Description

Get the first child node that is XML element node.

Demo Code


//package com.java2s;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**//from  w  w  w. j  a  va 2s .c  om
     * Get the first child node that is an element node.
     * 
     * @param node The node whose children should be iterated.
     * @return The first child element or {@code null}.
     */
    public static Element getFirstChildElement(Node node) {
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
                return (Element) children.item(i);
            }
        }

        return null;
    }
}

Related Tutorials