簡體   English   中英

如何使用jsoup從html元素中以字符串形式獲取html預覽結果?

[英]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&lt;bits/stdc++.h&gt; </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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM