Return the first XML Element instance among the child nodes of the element passed. - Java XML

Java examples for XML:XML Node Child

Description

Return the first XML Element instance among the child nodes of the element passed.

Demo Code


//package com.java2s;

import org.w3c.dom.*;

public class Main {
    /**//  w ww.j  a v a  2 s .  c om
     * Return the first Element instance among the child nodes of
     * the element passed.  If there are none, this will return a
     * null reference
     *
     * @return org.w3c.dom.Element
     * @param element org.w3c.dom.Element
     */
    public static Element firstChildElement(Element element) {
        if (element.hasChildNodes()) {
            NodeList children = element.getChildNodes();
            int count = children.getLength();
            for (int i = 0; i < count; ++i) {
                Node node = (Node) children.item(i);
                if (node instanceof Element)
                    return (Element) node;
            } // for
        } // if
        return null;
    }
}

Related Tutorials