Jsoup如何输出格式良好的XML?

时间:2014-01-07 15:58:39

标签: html xml jsoup

当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 那里的答案对我没有帮助,因为我必须接受我所得到的

1 个答案:

答案 0 :(得分:1)

问题在于解析时,因为jsoup正在创建3个属性:

content="y is "bad" here" 

并且属性的名称包含引号&#34;字符。 Jsoup确实转义了属性的值,但没有转义它的名称。

由于您是从字符串构建html doc,因此可能会在解析阶段出现错误。 有一种方法是将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:出乎意料的人物&#39; a&#39;在输入状态[AfterAttributeValue_quoted]中,40:意外字符&#39; &#39;在输入状态[AttributeName],46:意外字符&#39;&gt;&#39;在输入状态[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
}