富文本转文本-java过滤字符串html标签的几种方案

场景

公告内容是保存的是富文本, 有个地方要展示缩略内容, 过滤下标签

方案

方案一: JDK自带的HTML解析器

HTMLEditorKit.ParserCallback

import java.io.*;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;

/**
 * @author admin
 * @version 1.0
 * @date 2022/01/12 09:57
 */
public class Test extends HTMLEditorKit.ParserCallback {
    private static Test  test = new Test();
    private StringBuffer buff;

    private void parse(String str) throws IOException {
        InputStream iin = new ByteArrayInputStream(str.getBytes());
        Reader in = new InputStreamReader(iin);
        buff = new StringBuffer();
        ParserDelegator delegator = new ParserDelegator();
        delegator.parse(in, this, Boolean.TRUE);
        iin.close();
        in.close();
    }

    @Override
    public void handleText(char[] text, int pos) {
        buff.append(text);
    }

    private String getText() {
        return buff.toString();
    }

    public static String getContent(String str) {
        try {
            test.parse(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return test.getText();
    }

方案二: 正则过滤

    public static String htmlFilter(String htmlStr) {
        // 定义script的正则表达式
        String regExScript = "<script[^>]*?>[\\s\\S]*?</script>";
        // 定义style的正则表达式
        String regExStyle = "<style[^>]*?>[\\s\\S]*?</style>";
        // 定义HTML标签的正则表达式
        String regExHtml = "<[^>]+>";

        // 过滤script标签
        Pattern pScript = Pattern.compile(regExScript, Pattern.CASE_INSENSITIVE);
        Matcher mScript = pScript.matcher(htmlStr);
        htmlStr = mScript.replaceAll("");

        Pattern pStyle = Pattern.compile(regExStyle, Pattern.CASE_INSENSITIVE);
        Matcher mStyle = pStyle.matcher(htmlStr);
        htmlStr = mStyle.replaceAll("");

        Pattern pHtml = Pattern.compile(regExHtml, Pattern.CASE_INSENSITIVE);
        Matcher mHtml = pHtml.matcher(htmlStr);
        htmlStr = mHtml.replaceAll("");

        String str = htmlStr.trim();
        str = str.replaceAll("\\s*", "").replaceAll("", "");
        return str;
    }

方案三: 开源工具hutool

如果引入了hutool工具可以直接用里面的HtmlUtil  Hutool参考文档

System.out.println(HtmlUtil.cleanHtmlTag(content));

测试

找篇文章测试下: Idea使用SVN常用操作, f12复制网页内容

 复制出来的网页内容

测试方案一

    public static void main(String[] args) {
        String content = Test.getContent("复制过来的网页内容");
        System.out.println(content);
    }

 

测试方案二

System.out.println(htmlFilter(content));

 

测试方案三

 

总结

JDK自带的HTML解析器解析结果是最干净的, 方案二少过滤了&nbsp;, 方案三保留了一些换行格式, 总得来说问题不大,区别不大

扩展

没有。

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑶山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值