get the text value of XML element. - Java XML

Java examples for XML:XML Element Value

Description

get the text value of XML element.

Demo Code


//package com.java2s;

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

public class Main {
    /**// w  w  w  .ja  v a2 s  .c  om
     * This will get the text value of an element.
     *
     * @param node The node to get the text value for.
     * @return The text of the node.
     */
    public static String getNodeValue(Element node) {
        String retval = "";
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node next = children.item(i);
            if (next instanceof Text) {
                retval = next.getNodeValue();
            }
        }
        return retval;
    }
}

Related Tutorials