Java tutorial
//package com.java2s; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static String parseNodeAsText(Node node) { if (node == null) return null; String text = null; if (node.getNodeType() == 3) { text = node.getNodeValue(); } else { StringBuffer sb = new StringBuffer(); NodeList children = node.getChildNodes(); for (int i = 0; children != null && i < children.getLength(); i++) { Node child = children.item(i); if (child != null) { int childType = child.getNodeType(); if (childType == 3 || childType == 4 || childType == 8) { String childText = child.getNodeValue(); sb.append(childText); } else { sb.append(parseNodeAsText(child)); } } } if (sb.length() > 0) text = sb.toString(); } return text; } }