Returns specified XML element direct child node elements. - Android XML

Android examples for XML:XML Child Element

Description

Returns specified XML element direct child node elements.

Demo Code


//package com.java2s;

import java.util.LinkedList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**/*  www.  j a  va2 s .c om*/
     * Returns specified element direct child node elements.
     */
    public static List<Element> directChildren(Element parent,
            String childName) {
        final List<Element> result = new LinkedList<Element>();
        final NodeList nodeList = parent.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            final Node node = nodeList.item(i);
            if (node instanceof Element) {
                final Element element = (Element) node;
                if (element.getTagName().equals(childName)) {
                    result.add(element);
                }
            }
        }
        return result;
    }
}

Related Tutorials