Java HTML转换为纯文本 在Java中轻松将HTML格式文本转换为纯文本的方法示例(保留换行)
Piconjo_Official 人气:2第一步:引入Jsoup和lang和lang3的依赖:
Jsoup是HTML解析器
lang和lang3这两个包里有转换所需的工具类
<dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.11.3</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency>
第二步:直接使用即可:
import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.safety.Whitelist; /** * @author Piconjo */ public class Html2PlainText { public static String convert(String html) { if (StringUtils.isEmpty(html)) { return ""; } Document document = Jsoup.parse(html); Document.OutputSettings outputSettings = new Document.OutputSettings().prettyPrint(false); document.outputSettings(outputSettings); document.select("br").append("\\n"); document.select("p").prepend("\\n"); document.select("p").append("\\n"); String newHtml = document.html().replaceAll("\\\\n", "\n"); String plainText = Jsoup.clean(newHtml, "", Whitelist.none(), outputSettings); String result = StringEscapeUtils.unescapeHtml(plainText.trim()); return result; } }
使用测试:
加载全部内容