1

I want to generate

<td>&nbsp;</td>

using xom.

None of these work:

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"));
}

I get

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

any ideas?

Character encoding is set to "UTF-8" in my IDE.

4

1 回答 1

0

我建议您不要使用toXML() 但使用nu.xom.Serializer它通常会给出明确的数字实体引用。

序列化程序应该给出一个明确的数字实体引用 ( &#160;)。如果您真的需要&nbsp;,您可能必须继承 Serializer 并覆盖 Text 方法。

要使用序列化程序,请尝试:

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

如果您必须对 Serializer 进行子类化,它会变得更加棘手。

于 2013-08-06T14:13:51.147 回答