Java HTML to Text text(Element element)

Here you can find the source of text(Element element)

Description

text

License

Open Source License

Declaration

public static String text(Element element) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.regex.Pattern;
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 final Pattern WHITESPACE_BLOCK = Pattern.compile("[ \\t\\x0B\\f]+");

    public static String text(Element element) {
        final StringBuilder accum = new StringBuilder();
        new NodeTraversor(new NodeVisitor() {
            public void head(Node node, int depth) {
                if (node instanceof TextNode) {
                    TextNode textNode = (TextNode) node;
                    String str = textNode.getWholeText();
                    str = WHITESPACE_BLOCK.matcher(str).replaceAll(" ");
                    accum.append(str);//from   w  w  w . j a v a 2 s. c o  m
                }
            }

            public void tail(Node node, int depth) {
            }
        }).traverse(element);
        return accum.toString().trim();
    }
}

Related

  1. html2text(final String html)
  2. html2text(String html)
  3. html2text(String htmlStr)
  4. text(Element e)
  5. textOf(final Element el)
  6. toElement(String html)
  7. toHtmlByHtml(String html)
  8. toHtmlByPlain(String plainText)