【问题标题】:JSoup adding extra <br /> 'sJSoup 添加额外的 <br /> 的
【发布时间】:2012-10-24 12:40:14
【问题描述】:

JSoup 似乎在我的输出中添加了额外的 br 标记,如下所示。有没有办法阻止这种情况发生?

JUnit 测试:

@Test
public void testJsoup () throws MLException {
    String htmlBody = "<body> <div> <br class='calibre1'></br> <br class='calibre1'></br></div> </body>"; 
    Document doc = Jsoup.parse(htmlBody);
    htmlBody = doc.select("body").first().toString();
    System.out.println(htmlBody);
}

控制台输出:

<body> 
 <div> 
   <br class="calibre1" />
   <br /> 
   <br class="calibre1" />
   <br />
 </div> 
</body>

问候, 丹尼

【问题讨论】:

    标签: jsoup


    【解决方案1】:

    我在这里没有看到任何额外的&lt;br /&gt;-Tags ... 你是说换行吗?
    如果是,请看这里:jsoup line feed

    你可以做的是关闭prettyPrint

    final String html = "<body> <div> <br class='calibre1'></br> <br class='calibre1'></br></div> </body>";
    
    Document doc = Jsoup.parse(html);
    
    // This line will keep your Html in one line
    doc.outputSettings().prettyPrint(false);
    
    System.out.println(doc.body());
    

    输出:

    <body> <div> <br class="calibre1" /><br /> <br class="calibre1" /><br /></div> </body>
    

    【讨论】:

    • 嗨,Ollo,谢谢,但我在输出中有一个额外的
      。请注意,输入包含:“
      ”,但在输出中,它被转换为

      。换句话说,输入实际上是 1 个 br 标签(带有一个打开和关闭标签),但输出变成了 2 个 br 标签(都是自关闭的)。
    • 谢谢,没注意到。看起来有些事情无法通过更改某些设置来完成。这可能是一个错误吗?
    猜你喜欢
    • 2014-01-21
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多