Get the value of the specified XML element - Java XML

Java examples for XML:XML Element Value

Description

Get the value of the specified XML element

Demo Code


//package com.java2s;

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

public class Main {
    /**/*from   w  ww.  j  a v  a 2  s .c  o m*/
     * Get the value of the specified element
     *
     * @param element
     * @return
     */
    public static String getElementValue(Element element) {
        NodeList nl = element.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i).getNodeType() == Node.TEXT_NODE) {
                return element.getFirstChild().getNodeValue();
            }
        }

        return null;
    }
}

Related Tutorials