代码之家  ›  专栏  ›  技术社区  ›  wutzebaer

Jsoup添加了额外的换行符

  •  1
  • wutzebaer  · 技术社区  · 6 年前

    当我解析这一行html并将其转换回

    Jsoup.parse("h<span class='cool'>un</span>d", "").body().html()
    

    我在“h”之后有一条额外的新线

    "h\n<span class=\"cool\">un</span>d"
    

    我怎样才能避免这种情况发生?因为它在浏览器中显示时会增加额外的空间。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Luk    6 年前

    您可以通过设置禁用文档上的漂亮打印 doc.outputSettings().prettyPrint(false) :

    @Test
    public void testPrettyPrint() {
        String html = "h<span class='cool'>un</span>d";
        Document doc = Jsoup.parse(html, "");
    
        System.out.println(doc.body().html());
        System.out.println("==================");
        doc.outputSettings().prettyPrint(false);
        System.out.println(doc.body().html());
    }
    

    结果:

    h
    <span class="cool">un</span>d
    ==================
    h<span class="cool">un</span>d