Java HTML Jsoup Element convertNodeToText(Element element)

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

Description

convert Node To Text

License

Open Source License

Declaration

private static String convertNodeToText(Element element) 

Method Source Code

//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;
    }
}

Related

  1. childrenByTag(final Element element, final String tag)
  2. collectItemRewards(final Element icontab, final BiConsumer collector)
  3. collectTextUntilNextTag(final Element header, final String nextTagName)
  4. containsLink(Element element, String link)
  5. convertLinksToStrings(Elements links)
  6. convertTablesToDivs(Element body)
  7. copy(Element e)
  8. countElements(Node parent)
  9. createSafeElement(Element sourceEl)