简体   繁体   English

如何使用 Jsoup 仅从文本中删除 html 标签?

[英]How to remove only html tags from text with Jsoup?

I want to remove ONLY html tags from text with JSOUP.我只想使用 JSOUP 从文本中删除 html 标签。 I used solution from here ( my previous question about JSOUP ) But after some checkings I discovered that JSOUP gets JAVA heap exception: OutOfMemoryError for big htmls but not for all.我从这里使用了解决方案( 我之前关于 JSOUP 的问题)但是经过一些检查后我发现 JSOUP 得到 JAVA 堆异常:大 html 的 OutOfMemoryError 但不是全部。 For example, it fails on html 2Mb and 10000 lines.例如,它在 html 2Mb 和 10000 行上失败。 Code throws an exception in the last line (NOT on Jsoup.parse):代码在最后一行抛出异常(不在 Jsoup.parse 上):

public String StripHtml(String html){
  html = html.replace("&lt;", "<").replace("&gt;", ">");
  String[] tags = getAllStandardHtmlTags;
  Document thing = Jsoup.parse(html);
  for (String tag : tags) {
      for (Element elem : thing.getElementsByTag(tag)) {
          elem.parent().insertChildren(elem.siblingIndex(),elem.childNodes());
          elem.remove();
      }
  }
  return thing.html();
}

Is there a way to fix it?有办法解决吗?

Alternatively, you can give a try to Jsoup cleaning capabilities.或者,您可以尝试使用 Jsoup 清理功能。 The code below will remove ALL html tags located in the passed html string.下面的代码将删除位于传递的 html 字符串中的所有 html 标签。

public String StripHtml(String html) {
    return Jsoup.clean(html, Whitelist.none());
}

The whitelist ( Whitelist.none() ) tells the Jsoup cleaner which tags are allowed.白名单 ( Whitelist.none() ) 告诉 Jsoup 清理器允许哪些标签。 As you can see, none html tags are allowed here.如您所见,此处不允许使用任何 html 标记。 Any tags not referenced in the whitelist will be removed.白名单中未引用的任何标签都将被删除。

You may be interested by other provided whitelists:您可能对其他提供的白名单感兴趣:

Those base whitelists can be customized by adding tags (see addTags method) or by removing tags (see removeTags method).这些基础白名单可以通过添加标签(参见addTags方法)或删除标签(参见removeTags方法)来定制。

If you want to create your own whitelist (be careful,): here is the way to go:如果你想创建自己的白名单(小心,):这是要走的路:

Whitelist myCustomWhitelist = new Whitelist();
myCustomWhitelist.addTags("b", "em", ...);

See details here: Jsoup Whitelists在此处查看详细信息: Jsoup 白名单

Jsoup 1.8.3 Jsoup 1.8.3

After many searching in google and after some attempts to implement html stripper by myself, my solution is to use HTMLStripCharFilter class of Solr with replacing escapedTags to blackList with standard html tags .在 google 中多次搜索并尝试自己实现 html 剥离器之后,我的解决方案是使用Solr 的 HTMLStripCharFilter 类,escapedTags替换为blackList with standard html tags

  1. HTMLStripCharFilter is faster than JSOUP library and regexes for big size files HTMLStripCharFilter 比 JSOUP 库和大文件的正则表达式更快
  2. HTMLStripCharFilter hasn't memory problem like JSOUP (Out of memory exception) for big size files HTMLStripCharFilter 没有像 JSOUP(内存不足异常)这样的大文件内存问题
  3. HTMLStripCharFilter isn't entering to "catastrophic backtracking" like regexes HTMLStripCharFilter 没有像正则表达式那样进入“灾难性回溯”

I see two solutions:我看到两个解决方案:

  1. Increase the Java Heap space.增加 Java 堆空间。 It seems that generating the html as string needs more memory than you allow.似乎将 html 生成为字符串需要比您允许的更多的内存。 Increasing the maximum JAVA heap can be done with the -Xmx command line parameter to the JVM:可以使用 JVM 的-Xmx命令行参数来增加最大 JAVA 堆:

    java -Xmx512m parsing.java java -Xmx512m 解析.java

  2. You could switch from DOM based JSoup to a SAX based parser like nekohtml Such parsers can deal with any size html documents because they never build the complete DOM in memory.您可以从基于 DOM 的 JSoup 切换到基于 SAX 的解析器,如nekohtml这样的解析器可以处理任何大小的 html 文档,因为它们从不在内存中构建完整的 DOM。

for me was sufficient to use combination of Jsoup methods:对我来说,使用 Jsoup 方法的组合就足够了:

Jsoup.clean(Jsoup.parse(htmlString).text(), Whitelist.simpleText()) 

whitelist you may choose...您可以选择白名单...

Jsoup 1.14.2, just use: Jsoup 1.14.2,只需使用:

doc.select(cssQuery).text();

Strips all tags and returns a nice text content.剥离所有标签并返回漂亮的文本内容。 In my case I had a ul, li pairs, it nicely stripped them all.在我的例子中,我有一个 ul,li 对,它很好地剥离了它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM