2013-07-26 31 views
1

我要生成我怎樣才能把 到內容與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」。

回答

0

我建議你不要使用toXML(),而是使用nu.xom.Serializer,它通常會給出明確的數字實體引用。

串行器應該給出明確的數字實體引用(&#160;)。如果您確實需要&nbsp;,則可能必須繼承Serializer並覆蓋Text方法。

要使用串行嘗試:

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

,如果你要繼承它串行變得更加棘手。

+0

聽起來很有希望 - 你有沒有訪問任何示例代碼彼得? – OldCurmudgeon

+0

先嚐試序列化程序,看看它給了什麼。如果它給 這對你來說足夠好嗎? (所有瀏覽器和其他工具都應該處理它)。 –

+0

在序列化程序源代碼中插入一些暗示它也使用了「文本」,這也是問題的主要原因。我會看看我能解決什麼問題。現在我在'toXML'輸出上做了一個String.replace(「」,「&nbsp」)',但這是一個可怕的黑客。 – OldCurmudgeon