【问题标题】:How to remove HTML Entities in Jsoup?如何删除 Jsoup 中的 HTML 实体?
【发布时间】:2013-11-13 20:40:18
【问题描述】:

如何使用 Jsoup 删除 HTML 实体?如果我使用 Element.toString(),我会得到:

(...)
       <td>Letter &oacute;</td> //valid: <td>Letter ó</td>
(...)

【问题讨论】:

  • 但我想在 toString() 中获取文本“字母 ó”

标签: java html jsoup


【解决方案1】:

这可能与您的问题上下文无关,但如果您只想解码 HTML 实体而不对字符串进行任何其他更改(无标记处理、无注释剥离等),您可以使用 org.jsoup.parser.Parser.unescapeEntities ,例如:

assert Parser.unescapeEntities("x &asymp; <i>y</i>\n", true)
    .equals("x ≈ <i>y</i>\n");

【讨论】:

    【解决方案2】:

    我相信您可以在创建 Jsoup 文档时指定编码,如下所示:

    Document newDocument = Jsoup.parse(htmlString, StringUtils.EMPTY, Parser.htmlParser());
    newDocument.outputSettings().escapeMode(EscapeMode.base);
    newDocument.outputSettings().charset(CharEncoding.UTF_8);
    

    【讨论】:

    • EscapeMode.base 不适用于 óąś,但我设置为 xhtml :) 谢谢
    • 您需要使用这些设置,因为您可能会错过显示控制字符,例如 —例如(如果你当然关心那些:))
    • EscapeMode.base 是默认的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多