Return the first child XML element with a given name. - Java XML

Java examples for XML:XML Element Child

Description

Return the first child XML element with a given name.

Demo Code


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

public class Main {
    /**// w w w.  j a v  a 2 s .  c om
     * Return the first child element with a given name.  
     * @param parent parent element
     * @param name child element name
     * @return 
     */
    public static Element getFirstChildByName(Element parent, String name) {
        NodeList children = parent.getElementsByTagName(name);
        if (children.getLength() == 0) {
            return null;
        }
        return (Element) children.item(0);
    }
}

Related Tutorials