【问题标题】:How can Jsoup output well-formed XML?Jsoup如何输出格式良好的XML?
【发布时间】:2014-01-07 15:58:39
【问题描述】:

当 Jsoup 遇到某些类型的 HTML(复杂或不正确)时,它可能会发出格式错误的 HTML。一个例子是:

<html>
 <head>
  <meta name="x" content="y is "bad" here">
 </head>
 <body/>
</html>

引号应该被转义的地方。当 Jsoup 解析它时,它会发出:

<html>
 <head>
  <meta name="x" content="y is " bad"="" here"="" />
 </head>
 <body></body>
</html>

不符合 HTML 或 XML 标准。这是有问题的,因为它将在链下的下一个解析器中失败。

有什么方法可以确保 Jsoup 发出错误消息或(如 HtmlTidy)即使丢失了一些信息也能输出格式正确的 XML(毕竟我们现在无法确定什么是正确的)。

更新:失败的代码是:

    @Test
public void testJsoupParseMetaBad() {
    String s = "<html><meta name=\"x\" content=\"y is \"bad\" here\"><body></html>";
    Document doc = Jsoup.parse(s);
    String ss = doc.toString();
        Assert.assertEquals("<html> <head> <meta name=\"x\" content=\"y is \""
            +" bad\"=\"\" here\"=\"\" /> </head> <body></body> </html>", ss);
}

我正在使用:

    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.7.2</version>
    </dependency>

其他人似乎也有同样的问题: JSoup - Quotations inside attributes 那里的答案对我没有帮助,因为我必须接受给我的东西

【问题讨论】:

  • 由于 jSoup 是一个 DOM 解析器,它不应该这样做。从 DOM 重建 HTML 文本不会导致您显示的输出(恕我直言)。是否包含显示此行为的代码示例?
  • 我同意你的逻辑。我添加了用于解析上述“HTML”的代码。
  • @Tomalak 哪里说 jSoup 是一个 DOM 解析器(在 w3c 意义上)?本地名称似乎相同,但仅此而已。快速浏览源代码后,修复此问题可能需要更改代码。
  • 我还没有找到解决这个问题的方法,我也不相信 Jsoup 和 JTidy 一样有用和健壮。
  • 也许你应该简单地用测试用例报告一个问题:)

标签: html xml jsoup


【解决方案1】:

问题在于当你解析时,因为 jsoup 正在从以下位置创建 3 个属性:

content="y is "bad" here" 

并且属性的名称包含引号 " 字符。Jsoup 会转义属性的值,但不会转义其名称。

由于您是从字符串构建 html 文档,因此您可能会在解析阶段遇到错误。 有一种方法将 org.jsoup.parser.Parser 作为参数。默认解析方法不跟踪错误。

    String s = "<html><meta name=\"x\" content=\"y is \"bad\" here\"><body></html>";
    Parser parser = Parser.htmlParser(); // or Parser.xmlParser
    parser.setTrackErrors(100);
    Document doc = Jsoup.parse(s, "", parser);
    System.out.println(parser.getErrors());

输出:

[37:输入状态[AfterAttributeValue_quoted]中的意外字符'a',40:输入状态[AttributeName]中的意外字符'',46:输入状态[AttributeName]中的意外字符'>']

如果您不想更改解析并且只想获得有效的输出,您可以删除无效属性:

public static void fixIt(Document doc) {
    Elements els = doc.getAllElements();
    for(Element el:els){
        Attributes attributes = el.attributes();
        Set<String> remove = new HashSet<>();
        for(Attribute a:attributes){
            if(isForbidden(a.getKey())){
               remove.add(a.getKey());
            }
        }

        for(String k:remove){
            el.removeAttr(k);
        }
    }
}

public static boolean isForbidden(String el) {
    return el.contains("\""); //TODO
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-08
    • 2011-07-13
    • 1970-01-01
    • 2010-10-06
    • 2014-04-26
    • 2016-01-26
    • 2016-11-10
    相关资源
    最近更新 更多