get Text Content from XML Node - Java XML

Java examples for XML:DOM Node Value

Description

get Text Content from XML Node

Demo Code


//package com.java2s;

import org.w3c.dom.*;

public class Main {
    public static String getTextContent(Node n) {
        StringBuffer buffer = new StringBuffer();
        NodeList childList = n.getChildNodes();
        Node child;//from  w w  w.  ja v a 2 s. c om
        short nodetype;
        for (int i = 0; i < childList.getLength(); i++) {
            child = childList.item(i);
            nodetype = child.getNodeType();
            if (nodetype != Node.TEXT_NODE
                    && nodetype != Node.CDATA_SECTION_NODE)
                continue;
            buffer.append(child.getNodeValue());
        }
        return buffer.toString();
    }
}

Related Tutorials