get Value From XML Tag - Java XML

Java examples for XML:XML String

Description

get Value From XML Tag

Demo Code


//package com.java2s;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Main {
    public static void main(String[] argv) throws Exception {
        String tag = "java2s.com";
        System.out.println(getValueFromXMLTag(tag));
    }// w  w w. j  a  va  2 s.com

    public static Document document;

    /**
     * Die getValueFromXMLTag(String tag) Methode sorgt dafuer das anhand des tags der value zurueck gegeben wird.
     * @param tag
     * @return value
     */
    public static String getValueFromXMLTag(String tag) {
        NodeList ndList = document.getElementsByTagName(tag);
        String value = null;
        for (int i = 0; i < ndList.getLength(); i++) {
            Element element = (Element) ndList.item(i);
            value = element.getTextContent();
        }
        return value;
    }
}

Related Tutorials