get Child Elements By Name - Java XML

Java examples for XML:XML Element Child

Description

get Child Elements By Name

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Node;

public class Main {
    public static List<Node> getChildElementsByName(Node element,
            String name) {/*w  w w .  j av  a 2s  . c  o m*/
        ArrayList<Node> childList = new ArrayList<Node>();
        for (int i = 0; i < element.getChildNodes().getLength(); i++) {
            Node child = element.getChildNodes().item(i);
            if (Node.ELEMENT_NODE == child.getNodeType()
                    && name.equals(name)) {
                childList.add(child);
            }
        }
        return childList;
    }
}

Related Tutorials