将HTML字符串转换为字节数组Java。

huangapple 未分类评论42阅读模式
英文:

Convert HTML String to byte array java

问题

以下是翻译好的内容:

我想要将一个HTML字符串转换成字节数组,这样当我获取到字节数组时,它将保持HTML格式。我将普通的HTML字符串转换成了字节数组,但是当我检索它时,它只是HTML的纯文本,我希望它保持HTML格式。

final String html = "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "<style>\n" +
                "table {\n" +
                "  font-family: arial, sans-serif;\n" +
                "  border-collapse: collapse;\n" +
                "  width: 100%;\n" +
                "}\n" +
                "\n" +
                "td, th {\n" +
                "  border: 1px solid #dddddd;\n" +
                "  text-align: left;\n" +
                "  padding: 8px;\n" +
                "}\n" +
                "\n" +
                "tr:nth-child(even) {\n" +
                "  background-color: #dddddd;\n" +
                "}\n" +
                "</style>\n" +
                "</head>\n" +
                "<body>\n" +
                "\n" +
                "<h2>HTML Table</h2>\n" +
                "\n" +
                "<table>\n" +
                "  <tr>\n" +
                "    <th>Company</th>\n" +
                "    <th>Contact</th>\n" +
                "    <th>Country</th>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td>Alfreds Futterkiste</td>\n" +
                "    <td>Maria Anders</td>\n" +
                "    <td>Germany</td>\n" +
                "  </tr>\n" +
                "</table>\n" +
                "\n" +
                "</body>\n" +
                "</html>";
Spanned d  = Html.fromHtml(html);

我使用了 fromHTML() 将纯文本转换为了HTML,但是然后我无法将 Spanned 对象转换为字节数组。我应该如何做到这一点?

英文:

I want to convert an HTML string to a byte array so when I get the byte array it is in HTML format. I converting plain HTML string to byte array but when I retrieve it, it's just plain text of HTML but I want it to be formatted in HTML.

final String html = &quot;&lt;!DOCTYPE html&gt;\n&quot; +
                &quot;&lt;html&gt;\n&quot; +
                &quot;&lt;head&gt;\n&quot; +
                &quot;&lt;style&gt;\n&quot; +
                &quot;table {\n&quot; +
                &quot;  font-family: arial, sans-serif;\n&quot; +
                &quot;  border-collapse: collapse;\n&quot; +
                &quot;  width: 100%;\n&quot; +
                &quot;}\n&quot; +
                &quot;\n&quot; +
                &quot;td, th {\n&quot; +
                &quot;  border: 1px solid #dddddd;\n&quot; +
                &quot;  text-align: left;\n&quot; +
                &quot;  padding: 8px;\n&quot; +
                &quot;}\n&quot; +
                &quot;\n&quot; +
                &quot;tr:nth-child(even) {\n&quot; +
                &quot;  background-color: #dddddd;\n&quot; +
                &quot;}\n&quot; +
                &quot;&lt;/style&gt;\n&quot; +
                &quot;&lt;/head&gt;\n&quot; +
                &quot;&lt;body&gt;\n&quot; +
                &quot;\n&quot; +
                &quot;&lt;h2&gt;HTML Table&lt;/h2&gt;\n&quot; +
                &quot;\n&quot; +
                &quot;&lt;table&gt;\n&quot; +
                &quot;  &lt;tr&gt;\n&quot; +
                &quot;    &lt;th&gt;Company&lt;/th&gt;\n&quot; +
                &quot;    &lt;th&gt;Contact&lt;/th&gt;\n&quot; +
                &quot;    &lt;th&gt;Country&lt;/th&gt;\n&quot; +
                &quot;  &lt;/tr&gt;\n&quot; +
                &quot;  &lt;tr&gt;\n&quot; +
                &quot;    &lt;td&gt;Alfreds Futterkiste&lt;/td&gt;\n&quot; +
                &quot;    &lt;td&gt;Maria Anders&lt;/td&gt;\n&quot; +
                &quot;    &lt;td&gt;Germany&lt;/td&gt;\n&quot; +
                &quot;  &lt;/tr&gt;\n&quot; +
                &quot;&lt;/table&gt;\n&quot; +
                &quot;\n&quot; +
                &quot;&lt;/body&gt;\n&quot; +
                &quot;&lt;/html&gt;\n&quot;;
        Spanned d  = Html.fromHtml(html);

I converted the plaintext to HTML using fromHTML() but then I'm unable to convert spanned to byte array. How can I do this?

答案1

得分: 0

将依赖项添加到您的 pom.xml 文件中:

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.12.1</version>
</dependency>

然后,您可以使用 Jsoup 实用类的 parse 方法来对您的 HTML 字符串进行漂亮的打印:

System.out.println(Jsoup.parse(html).toString());
英文:

Add a dependency to JSOUP to your pom.xml :

&lt;dependency&gt;
    &lt;groupId&gt;org.jsoup&lt;/groupId&gt;
    &lt;artifactId&gt;jsoup&lt;/artifactId&gt;
    &lt;version&gt;1.12.1&lt;/version&gt;
&lt;/dependency&gt;

Then you can pretty print your HTML string using the parse method of the Jsoup utility class:

System.out.println(Jsoup.parse(html).toString());

huangapple
  • 本文由 发表于 2020年7月25日 20:53:50
  • 转载请务必保留本文链接:https://java.coder-hub.com/63088543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定