Here you can find the source of getFormattedText(Element element)
Parameter | Description |
---|---|
element | An html element |
public static String getFormattedText(Element element)
//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(); } }