以下代码将此文本替换为:<br />

String removeDisallowedTags(String textToEscape) {
    Whitelist whitelist = Whitelist.none();
    whitelist.addTags(new String[] { "b", "br", "font" });

    String safe = Jsoup.clean(textToEscape, whitelist);
    return safe;
}

为什么?

最佳答案

Jsoup.clean()默认情况下将文档处理为html,并且在html<br>中不允许关闭标记。<img>也是如此。
您必须将代码解析为XML。这会让标签关闭-甚至会为你关闭它们。有一些附加设置的固定方法:

String cleanXmlAndRemoveUnwantedTags(String textToEscape) {
    Whitelist whitelist = Whitelist.none();
    whitelist.addTags(allowedTags);

    OutputSettings outputSettings = new OutputSettings()
                    .syntax(OutputSettings.Syntax.xml)
                    .charset(StandardCharsets.UTF_8)
                    .prettyPrint(false);

    String safe = Jsoup.clean(textToEscape, "", whitelist, outputSettings);
    return safe;
}

09-26 16:23