Extracts the text from the given XML element. - Android XML

Android examples for XML:XML Element

Description

Extracts the text from the given XML element.

Demo Code


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

public class Main {
    /**// w w w .j av  a  2 s .  c  o m
     * Extracts the text from the given element.
     * Element.getTextContet() is java5 specific, so we need to use this until we drop 1.4 support.
     */
    public static String getTextContent(Node element) {
        StringBuffer text = new StringBuffer();
        NodeList childNodes = element.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if (child instanceof Text) {
                text.append(child.getNodeValue());
            }
        }

        return text.toString();
    }
}

Related Tutorials