get Child XML Elements - Android XML

Android examples for XML:XML Child Element

Description

get Child XML Elements

Demo Code

/**//  w w w.j a v a 2s.co m
 * Copyright (c) 2009 Aurora Software Technology Studio. All rights reserved.
 */
//package com.java2s;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * @param parent the parent XML element
     * @param childName the child node name
     * @return the list of child XML elements with specified node name
     */
    public static List<Element> getChildElements(final Element parent,
            final String childName) {

        if (parent != null) {

            List<Element> result = new ArrayList<Element>();
            NodeList nodes = parent.getElementsByTagName(childName);
            for (int i = 0, count = nodes.getLength(); i < count; i++) {
                result.add((Element) nodes.item(i));
            }
            return result;
        } else {
            return null;
        }
    }
}

Related Tutorials