Java HTML to Text textOf(final Element el)

Here you can find the source of textOf(final Element el)

Description

text Of

License

Open Source License

Declaration

static String textOf(final Element el) 

Method Source Code

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

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 {
    static String textOf(final Element el) {
        final StringBuilder accum = new StringBuilder();
        new NodeTraversor(new NodeVisitor() {
            public void head(final Node node, final int depth) {
                if (node instanceof TextNode) {
                    TextNode textNode = (TextNode) node;
                    accum.append(textNode.text());
                } else if (node instanceof Element) {
                    Element element = (Element) node;
                    if (element.tag().getName().equals("br")) {
                        accum.append("\n");
                    }// w w w.  j av a  2s  . co m
                }
            }

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

Related

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