get XML Element By Tag Name - Android XML

Android examples for XML:XML Element

Description

get XML Element By Tag Name

Demo Code


//package com.java2s;

import java.util.ArrayList;

import java.util.List;

import org.w3c.dom.Element;

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

public class Main {
    public static Element getElementByTagName(Element element, String name) {
        NodeList nodeList = element.getElementsByTagName(name);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) node;
            }/*from  w w  w . j  a v  a2  s  . c  o  m*/
        }
        return null;
    }

    public static List<Element> getElementsByTagName(Element element,
            String name) {
        List<Element> elementList = new ArrayList<Element>();
        NodeList nodeList = element.getElementsByTagName(name);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                elementList.add((Element) node);
            }
        }
        return elementList;
    }
}

Related Tutorials