get XML Children Of Name - Java XML

Java examples for XML:XML Element Child

Description

get XML Children Of Name

Demo Code


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

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

public class Main {
    public static List<Node> getChildrenOfName(Node node, String name) {
        List<Node> res = new ArrayList<Node>();
        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node item = childNodes.item(i);

            String localName = item.getNodeName();
            if (name.equals(localName)) {
                res.add(item);/*from   w  ww  .j av  a  2s  .  c o m*/
            }
        }
        return res;
    }
}

Related Tutorials