get Children Elements By XML Tag - Java XML

Java examples for XML:XML Element Child

Description

get Children Elements By XML Tag

Demo Code


//package com.java2s;

import java.util.Vector;
import org.w3c.dom.Element;

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

public class Main {
    public static Vector getChildrenElementsByTag(Node node, String name) {
        Vector result = new Vector();
        NodeList nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++)
            if (nl.item(i) instanceof Element
                    && (((Element) nl.item(i)).getTagName().equals(name) || name == null)) {
                result.add(nl.item(i));//from ww w  .j  av  a2 s  .c om
                //System.out.println(nl.item(i).toString());
                //System.out.println(((Element)nl.item(i)).getAttribute("className"));
                //System.out.println(((Element)nl.item(i)).getTagName());
            }
        return result;
    }
}

Related Tutorials