get Node Values from XML Element by name - Android XML

Android examples for XML:XML Node

Description

get Node Values from XML Element by name

Demo Code


//package com.java2s;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static String[] getNodeValues(Element element, String name) {
        NodeList list = element.getElementsByTagName(name);
        if (list == null)
            return null;
        String[] result = new String[list.getLength()];
        for (int i = 0; i < result.length; i++)
            result[i] = list.item(i).getFirstChild().getNodeValue().trim();
        return result;
    }//from w w  w. j  a  v  a2  s.c  o m

    public static String getNodeValue(Element element, String name,
            int index) {
        NodeList list = element.getElementsByTagName(name);
        if (list == null)
            return null;
        Node node = list.item(index).getFirstChild();
        if (node == null)
            return null;
        return node.getNodeValue().trim();
    }

    public static String getNodeValue(Node element, String name) {
        return getNodeValue((Element) element, name, 0);
    }

    public static String getNodeValue(Element element, String name) {
        return getNodeValue(element, name, 0);
    }
}

Related Tutorials