String source = "<h1> ... </h1> <div> .... </div> <span>...</span>";
Document doc = Jsoup.parseBodyFragment(source);
Elements elements = doc.select("div");
for (Element element : elements) {
element.replaceWith(new TextNode(element.toString(),""));
}
Whitelist whitelist = new Whitelist();
whitelist.addTags("a").addAttributes("a", "href", "name", "rel", "target");
whitelist.addTags("h1","h2","h3","h5","h6","strong","u","i","ul","li");
String clean = Jsoup.clean(doc.body().toString(), whitelist);
System.out.println(clean);
输出
<h1> ... </h1> <div> .... </div> ...
我在期待
<h1> ... </h1> <div> .... </div> <span> .... </span>
即将所有html标记编码为<
>
,但我已添加到白名单。我不想删除标签。
我是否必须指定它应编码为<
>
的每个标记,还是有其他方式?
我缺少什么?