美好的一天,每個人。我試圖在數據庫中保存HTML代碼,我使用的是SHEF(Swing HTML Editor Framework),但是我遇到了一個很大的問題。通常情況下,生成的HTML是這樣的:如何使用JSoup或HTMLCleaner縮短HTML代碼
<div>
This is the first paragraph
</div>
<div>
This is the second paragraph.
</div>
<div>
This is the last paragraph.
</div>
我要「乾淨」的HTML代碼,使結果看起來是這樣,而不是:
<div>
This is the first paragraph
<br>
This is the second paragraph.
<br>
This is the last paragraph.
</div>
我試圖用HTMLCleaner和JSoup,但我沒有做到。我只能JSoup工作,使得
<div>
This is the first paragraph
</div>
<div>
</div>
<div>
This is the last paragraph.
</div>
成爲
<div>
This is the first paragraph
</div>
<br>
<div>
This is the last paragraph.
</div>
這是我用JSoup代碼:
Document source = Jsoup.parse(sourceString);
// For each element
for(Element el: source.select("*")) {
if(el.children().isEmpty() && !el.hasText() && el.isBlock()) {
el.replaceWith(new Element(Tag.valueOf("br"), ""));//replace empty tags with newline
}
}
return source.body().html();
有什麼辦法使生成的HTML代碼短?謝謝!
清理/編輯HTML與Swing無關。不要僅因爲應用程序添加Swing標記。使用一些Swing API。 –