Jsoup:对标签进行编码,不要删除标签

时间:2014-10-06 07:40:37

标签: jsoup

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> &lt;div&gt; .... &lt;/div&gt; ...

我在期待

<h1> ... </h1> &lt;div&gt; .... &lt;/div&gt; &lt;span&gt; .... &lt;/span&gt;

即将所有html标记编码为&lt; &gt;,但我已添加到白名单。我不想删除标签。

我是否必须指定它应编码为&lt; &gt;的每个标记,还是有其他方式?

我缺少什么?

0 个答案:

没有答案