如何在Java中删除HTML标签 [英] How to remove HTML tag in Java

查看:115
本文介绍了如何在Java中删除HTML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可以完全移除HTML标签的正则表达式?
顺便说一下,我使用的是Java。

Is there the regular expression that can completely remove a HTML tag? By the way, I'm using Java.

推荐答案

您应该使用HTML解析器。我喜欢 htmlCleaner ,因为它给了我一个非常漂亮的HTML版本。

You should use a HTML parser instead. I like htmlCleaner, because it gives me a pretty printed version of the HTML.

使用htmlCleaner你可以这样做:

With htmlCleaner you can do:

TagNode root = htmlCleaner.clean( stream );
Object[] found = root.evaluateXPath( "//div[id='something']" );
if( found.length > 0 && found instanceof TagNode ) {
    ((TagNode)found[0]).removeFromTree();
}

这篇关于如何在Java中删除HTML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆