get Children By XML Tag Name - Java XML

Java examples for XML:XML Element Child

Description

get Children By XML Tag Name

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    public static List<Element> getChildrenByTagName(Element parent,
            String name) {/*  w w  w  . ja  va 2 s .c o m*/
        List<Element> nodeList = new ArrayList<Element>();
        for (Node child = parent.getFirstChild(); child != null; child = child
                .getNextSibling()) {
            if (child.getNodeType() == Node.ELEMENT_NODE
                    && name.equals(child.getNodeName())) {
                nodeList.add((Element) child);
            }
        }

        return nodeList;
    }
}

Related Tutorials