[英]How to get html preview result as a string from html element with jsoup?
我想從jsoup元素獲取HTML預覽結果。 假設我有具有以下html代碼的jsoup元素:
元素的HTML代碼:
<div class="code-container">
<div id="highlighter_245626" class="syntaxhighlighter nogutter night">
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="comments">// C++ program for implementation of FCFS </code></div>
<div class="line number2 index1 alt1"><code class="comments">// scheduling </code></div>
<div class="line number3 index2 alt2"><code class="preprocessor">#include<bits/stdc++.h> </code></div>
<div class="line number4 index3 alt1"><code class="keyword bold">using</code> <code class="keyword bold">namespace</code> <code class="plain">std; </code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div></div>
HTML預覽結果字符串:
// C++ program for implementation of FCFS
#include<bits/stdc++.h>
using namespace std;
我嘗試使用Element.Text()獲取HTML預覽字符串,但遇到以下問題:
是否有更好的方法使用jsoup從HTML元素獲取HTML預覽結果作為字符串?
這將為您保留換行符:
public static String cleanPreserveLineBreaks(String bodyHtml) {
// get pretty printed html with preserved br and p tags
String prettyPrintedBodyFragment = Jsoup.clean(bodyHtml, "", Whitelist.none().addTags("br", "p"), new OutputSettings().prettyPrint(true));
// get plain text with preserved line breaks by disabled prettyPrint
return Jsoup.clean(prettyPrintedBodyFragment, "", Whitelist.none(), new OutputSettings().prettyPrint(false));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.