Returns a node's first child XML node that is an element. - Java XML

Java examples for XML:XML Element Child

Description

Returns a node's first child XML node that is an element.

Demo Code


//package com.java2s;
import org.w3c.dom.*;

public class Main {
    /**//from   ww  w. j  a v  a 2s . co m
     * Returns a node's first child node that is an element.
     * @param parent
     * @return first child element, or null
     */
    public static Element getFirstChildElement(Node parent) {
        NodeList children = parent.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node n = children.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) n;
            }
        }
        return null;
    }
}

Related Tutorials