输入文字:
<p>Arbit string <b>of</b><br><br>text. <em>What</em> to <strong>do</strong> with it?
我运行以下代码:
Whitelist list = Whitelist.simpleText().addTags("br");
// Some other code...
// plaintext is the string shown above
retVal = Jsoup.clean(plaintext, StringUtils.EMPTY, list,
new Document.OutputSettings().prettyPrint(false));
我得到了输出:
Arbit string <b>of</b>
text. <em>What</em> to <strong>do</strong> with it?
我不希望Jsoup将<br>
标记转换为换行符,我希望将它们保持原样。我怎么能这样做?
答案 0 :(得分:2)
试试这个:
Document doc2deal = Jsoup.parse(inputText);
doc2deal.select("br").append("br"); //or append("<br>")
答案 1 :(得分:0)
这对我来说是不可复制的。使用Jsoup 1.8.3和此代码:
String html = "<p>Arbit string <b>of</b><br><br>text. <em>What</em> to <strong>do</strong> with it?";
String cleaned = Jsoup.clean(html,
"",
Whitelist.simpleText().addTags("br"),
new Document.OutputSettings().prettyPrint(false));
System.out.println(cleaned);
我得到以下输出:
Arbit string <b>of</b><br><br>text. <em>What</em> to <strong>do</strong> with it?
我猜你的问题必定在其他地方。