Java tutorial
//package com.java2s; //License from project: Apache License import org.w3c.dom.Node; public class Main { /** * @param n the node to look for text on * @return The conjoined values of all #text nodes immediately under this node */ public static String getText(Node n) { return getTextWithoutTrim(n).trim(); } public static String getTextWithoutTrim(Node n) { StringBuffer sb = new StringBuffer(); for (Node ele = n.getFirstChild(); ele != null; ele = ele.getNextSibling()) { String name = ele.getNodeName(); if (name.equalsIgnoreCase("#text")) sb.append(ele.getNodeValue()); } return sb.toString(); } }