I want to remove ONLY html tags from text with JSOUP. 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. For example, it fails on html 2Mb and 10000 lines. Code throws an exception in the last line (NOT on Jsoup.parse):
public String StripHtml(String html){
html = html.replace("<", "<").replace(">", ">");
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. The code below will remove ALL html tags located in the passed html string.
public String StripHtml(String html) {
return Jsoup.clean(html, Whitelist.none());
}
The whitelist (Whitelist.none()
) tells the Jsoup cleaner which tags are allowed. As you can see, none html tags are allowed here. 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).
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 1.8.3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With