如何从字符串中删除html元素

时间:2014-07-24 07:26:15

标签: java javascript cleditor

BO:

class Apple {

private String appleName;

private String appleBenefits;

}

从jsp,我使用jquery编辑器插件(cleditor.js)来输入字段"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>"

但是当我需要将其值存储在DB中时,我需要将其存储为:

"An apple a day keeps the Doctor away"

我怎样才能在java中做到这一点?

1 个答案:

答案 0 :(得分:4)

您可以使用jsoup从字符串中删除html元素,也可以使用xss security

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

并删除html,试试这个:

Jsoup.parse(html).text();

编辑:感谢user2640782

使用此代码,输出将是“每天一个苹果让医生离开”。要删除开头的空白区域,可以这样调用:Jsoup.parse(appleBenefits).text().replace(String.valueOf((char) 160), " ").trim();