2012-10-24 54 views
1

JSoup似乎正在为我的输出添加额外的br标签,如下所示。有没有办法阻止这种情况发生?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> 

问候, 丹尼

回答

2

我没有看到任何多余的<br />标签都有效这里...你的意思线反馈?
如果是,看看这里: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> 
+1

OLLO您好,感谢,但我输出中有一个额外的
。请注意,输入包含以下内容:“

”,但在输出中,它被转换为

。换句话说,输入实际上是1个标记(带有打开和关闭标记),但是输出变成了2个br标记(都是自闭合的)。 –

+1

谢谢,没有注意到。看起来像改变某些设置无法完成的事情。这可能是一个错误? – ollo