You can try using the JSoup library. This API provides a clean method:
For an example, look here: Sanitize untrusted HTML :
String unsafe = "<p><a href='http://example.com/' onclick='stealCookies()'>Link</a></p>"; String safe = Jsoup.clean(unsafe, Whitelist.basic()); // now: <p><a href="http://example.com/" rel="nofollow">Link</a></p>
source share