在Java中轻松将HTML格式文本转换为纯文本(保留换行)

1,881 阅读1分钟

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

Jsoup是HTML解析器lang和lang3这两个包里有转换所需的工具类

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

#使用测试:

感谢阅读 喜欢学习的朋友们可以关注下小编 小编会定期更新优质文章。