Here you can find the source of convertNodeToText(Element element)
private static String convertNodeToText(Element element)
//package com.java2s; import org.jsoup.nodes.Element; import org.jsoup.nodes.Node; import org.jsoup.nodes.TextNode; import org.jsoup.select.NodeTraversor; import org.jsoup.select.NodeVisitor; public class Main { private static String convertNodeToText(Element element) { if (element == null) return ""; final StringBuilder buffer = new StringBuilder(); new NodeTraversor(new NodeVisitor() { @Override/*from www . j a v a2 s. com*/ public void head(Node node, int depth) { if (node instanceof TextNode) { TextNode textNode = (TextNode) node; String text = textNode.text().replace('\u00A0', ' ').trim(); // non breaking space if (!text.isEmpty()) { buffer.append(text); if (!text.endsWith(" ")) { buffer.append(" "); // the last text gets appended the extra space too but we remove it later } } } } @Override public void tail(Node node, int depth) { } }).traverse(element); String output = buffer.toString(); if (output.endsWith(" ")) { // removal of the last extra space output = output.substring(0, output.length() - 1); } return output; } }