get XML Sub Nodes - Java XML

Java examples for XML:XML Node Child

Description

get XML Sub Nodes

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 final List<Node> getSubNodes(Node n, String tag) {
        NodeList children;//w w  w.  ja  va  2 s  .  c o m
        Node childnode;

        List<Node> nodes = new ArrayList<Node>();

        if (n == null)
            return nodes;

        children = n.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            childnode = children.item(i);
            if (childnode.getNodeName().equalsIgnoreCase(tag)) // <file>
            {
                nodes.add(childnode);
            }
        }
        return nodes;
    }
}

Related Tutorials