久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

在Java中輕松將HTML格式文本轉(zhuǎn)換為純文本的方法示例(保留換行)

瀏覽:136日期:2022-09-02 18:42:15

第一步:引入Jsoup和lang和lang3的依賴:

Jsoup是HTML解析器lang和lang3這兩個(gè)包里有轉(zhuǎn)換所需的工具類

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

使用測(cè)試:

在Java中輕松將HTML格式文本轉(zhuǎn)換為純文本的方法示例(保留換行)

在Java中輕松將HTML格式文本轉(zhuǎn)換為純文本的方法示例(保留換行)

到此這篇關(guān)于在Java中輕松將HTML格式文本轉(zhuǎn)換為純文本的方法示例(保留換行)的文章就介紹到這了,更多相關(guān)Java HTML轉(zhuǎn)換為純文本內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 柘荣县| 皋兰县| 泰州市| 大关县| 滁州市| 富平县| 苗栗县| 石柱| 台湾省| 丹凤县| 宝应县| 叶城县| 伊川县| 礼泉县| 赣榆县| 乌苏市| 彭泽县| 福海县| 建德市| 儋州市| 天祝| 青州市| 色达县| 涿鹿县| 福州市| 施甸县| 乌兰浩特市| 萝北县| 奉新县| 开化县| 新野县| 孟津县| 会昌县| 平顺县| 哈密市| 古浪县| 汨罗市| 福建省| 贵州省| 东海县| 民乐县|