javajavascriptcleditor

How to remove html elements from a string


BO:

class Apple {

private String appleName;

private String appleBenefits;

}

From the jsp, I am using a jquery editor plugin (cleditor.js) to type in the value of the field "appleBenefits" from the jsp.

String appleBenefits = "<ul><li>&nbsp;&nbsp;&nbsp;&nbsp;An apple a day keeps the <hr><font color="ff0000">Doctor away</li></ui><br>"

However when I need to store its value in DB, I need to store it as:

"An apple a day keeps the Doctor away"

How can I do this in java?


Solution

  • You can use jsoup for removing html elements from string and also for xss security

    http://jsoup.org/cookbook/cleaning-html/whitelist-sanitizer

    and to just remove html, try this:

    Jsoup.parse(html).text();
    

    EDIT: thanks to user2640782

    With this code the output will be " An apple a day keeps the Doctor away". To get rid of the empty spaces in the beginning you can call like this: Jsoup.parse(appleBenefits).text().replace(String.valueOf((char) 160), " ").trim();