[英]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.