Here you can find the source of getTextContent(Node n)
public static String getTextContent(Node n) throws DOMException
//package com.java2s; import org.w3c.dom.DOMException; import org.w3c.dom.Node; public class Main { public static String getTextContent(Node n) throws DOMException { Node child = n.getFirstChild(); if (child != null) { Node next = child.getNextSibling(); if (next == null) { return hasTextContent(child) ? child.getNodeValue() : ""; }/* w w w. j a v a 2 s .c om*/ StringBuffer buf = new StringBuffer(); getTextContent(n, buf); return buf.toString(); } return ""; } public static void getTextContent(Node n, StringBuffer buf) throws DOMException { Node child = n.getFirstChild(); while (child != null) { if (hasTextContent(child)) { String content = child.getNodeValue(); if (content != null) { buf.append(content); } } child = child.getNextSibling(); } } public static boolean hasTextContent(Node child) { return child.getNodeType() != Node.COMMENT_NODE && child.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE; } }