<dependency>
<groupId>net.htmlparser.jericho</groupId>
<artifactId>jericho-html</artifactId>
<version>3.4</version>
</dependency>
public String cleanHtml(String html) {
Source source = new Source(html);
OutputDocument outputDocument = new OutputDocument(source);
outputDocument.remove(new ElementFilter("script"));
outputDocument.remove(new ElementFilter("style"));
for (Element element : source.getAllElements()) {
if (!element.getStartTag().getName().equals("a")) {
element.getStartTag().removeAttribute("onclick");
element.getStartTag().removeAttribute("class");
}
}
return outputDocument.toString();
}
public static void main(String[] args) {
String cleanedHtml = cleanHtml(htmlText);
System.out.println(cleanedHtml);
}