get Child XML Nodes by tag name - Java XML

Java examples for XML:XML Node Child

Description

get Child XML Nodes by tag name

Demo Code


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

public class Main {
    public static Node[] getChildNodes(Node parent, String tagName) {
        List<Node> nodeList = new ArrayList<Node>();
        NodeList ndList = parent.getChildNodes();
        int length = ndList.getLength();
        for (int i = 0; i < length; i++) {
            Node n = ndList.item(i);
            if (n instanceof Text)
                continue;
            if (n.getNodeName().equals(tagName))
                nodeList.add(n);/* ww w  .  j a  v a  2 s  .com*/
        }
        return nodeList.toArray(new Node[0]);
    }
}

Related Tutorials