Here you can find the source of nodeToText(Node node)
private static String nodeToText(Node node)
//package com.java2s; //License from project: LGPL import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; public class Main { private static String nodeToText(Node node) { StringBuffer buffer = new StringBuffer(); if (node instanceof Text) { Text text = (Text) node; buffer.append(text.getData()); buffer.append(' '); } else {/* ww w .jav a 2 s.c o m*/ NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { buffer.append(nodeToText(list.item(i))); buffer.append(' '); } } String ret = buffer.toString(); return ret.replaceAll("\\s+", " ").trim(); } }