Java HTML Jsoup Element getFormattedText(Element element)

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

Description

Given a jsoup element, gets all contained text preserving formatting by tags such as

License

BSD License

Parameter

Parameter Description
element An html element

Return

String

Declaration

public static String getFormattedText(Element element) 

Method Source Code


//package com.java2s;
//License from project: BSD License 

import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;

public class Main {
    /**/* ww w . ja va 2  s. c o  m*/
     * Given a jsoup element, gets all contained text preserving formatting by tags such as <br>
     * @param element An html element
     * @return String
     */
    public static String getFormattedText(Element element) {
        StringBuilder stringBuilder = new StringBuilder();
        element.childNodes().forEach(node -> {
            if (node instanceof TextNode) {
                stringBuilder.append(((TextNode) node).text());
            } else if (node instanceof Element) {
                if ("br".equalsIgnoreCase(((Element) node).tag().getName())) {
                    stringBuilder.append("\n");
                } else {
                    stringBuilder.append(getFormattedText((Element) node));
                }
            }
        });
        return stringBuilder.toString();
    }
}

Related

  1. getElementsFirstLevel(String html)
  2. getElementTextBySelector(String html, String selector)
  3. getElementValue(Element wrapperElement, String cssQuery)
  4. getElIndexInSameTags(Element e)
  5. getFirstWithOwnText(final Elements elementList, final String text)
  6. getIndustry(Element element)
  7. getInt(Elements td, int i)
  8. getLink(Element element, int index)
  9. getMoney(final Element container)