Here you can find the source of getText(Element el)
public static String getText(Element el)
//package com.java2s; //License from project: Open Source License import java.util.StringTokenizer; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.w3c.dom.Text; public class Main { /**//ww w . j a v a 2 s .c o m * get the text string in an element (eg interspersed between child elements), * or "" if there is none or if the Element is null. * Tries to ignore white space text; but does not succeed. */ public static String getText(Element el) { String res = ""; if (el != null) try { el.normalize(); // does not help recognise white space NodeList nodes = el.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) if (nodes.item(i) instanceof Text) { Text text = (Text) nodes.item(i); // this filter seems to make no difference if (!text.isElementContentWhitespace()) { String tData = text.getData(); // this seems to be an effective way to catch pure white space StringTokenizer nonWhiteSpace = new StringTokenizer(tData, "\n \t"); if (nonWhiteSpace.countTokens() > 0) res = res + tData; } } } catch (Exception e) { System.out.println("Text failure: " + e.getMessage()); } return res; } }