簡體   English   中英

如何使用XOM將內容放入內容

[英]How can I put a   into the content with xom

我想產生

<td>&nbsp;</td>

使用xom

這些都不起作用:

private static void test(String s) {
  Element e = new Element("td");
  e.appendChild(s);
  System.out.println("XML(\"" + s + "\"): " + e.toXML());
}

private static void test() throws UnsupportedEncodingException {
  final String nbsp = "\u00A0";
  final String nbsp2 = "\uC2A0";
  final String nbsp3 = "&#038;nbsp;";
  test(nbsp);
  test(nbsp2);
  test(nbsp3);
  test("&nbsp;");
  final byte[] b = nbsp.getBytes("UTF-8");
  test(new String(b, "UTF-8"));
}

我懂了

XML(" "): <td> </td>
XML("슠"): <td>슠</td>
XML("&#038;nbsp;"): <td>&amp;#038;nbsp;</td>
XML("&nbsp;"): <td>&amp;nbsp;</td>
XML(" "): <td> </td>

有任何想法嗎?

在我的IDE中,字符編碼設置為“ UTF-8”。

我建議您不要使用toXML()而應使用nu.xom.Serializer ,它通常會提供明確的數字實體引用。

序列化程序應提供明確的數字實體引用( &#160; )。 如果您真的需要&nbsp; 您可能必須繼承Serializer並重寫Text方法。

要使用序列化器,請嘗試:

    OutputStream out = new FileOutputStream(file);
    Serializer ser = new Serializer(out);
    ser.write(doc);
    out.close();

如果必須將Serializer子類化,它將變得更加棘手。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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