当涉及代理对时,我遇到了 marshaller 的一种奇怪行为。为什么 JAXB marshaller 添加不必要的(和无效的)XML 实体?当我尝试编组以下内容时:
- \uD83D\uDCB3,例如 55357 56499 个代码点
Mashaller 输出 128179 代码点(有效并代表 XML 中的代理对)和不必要的 56499(不是有效的 XML 实体,代表对的低部分)。如何配置编组器以在输出中实现有效的 XML 实体,还是只需要升级库?我正在使用 Java 8。
示例重现代码:
String inputSurrogate = "\uD83D\uDCB3";
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
StringWriter sw = new StringWriter();
Customer customer = new Customer();
customer.setText(inputSurrogate);
jaxbMarshaller.marshal(customer, sw);
String xmlString = sw.toString();
System.out.println(xmlString);
for (int i = 0; i < xmlString.length(); i++) {
int ch = xmlString.codePointAt(i);
System.out.print(ch);
System.out.print("|");
}
输出(注意 |128179|56499|,56499 在我的理解中是不必要且无效的):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer>
<text></text>
</customer>
60|63|120|109|108|32|118|101|114|115|105|111|110|61|34|49|46|48|34|32|101|110|99|111|100| 105|110|103|61|34|85|84|70|45|56|34|32|115|116|97|110|100|97|108|111|110|101|61|34|121| 101|115|34|63|62|10|60|99|117|115|116|111|109|101|114|62|10|32|32|32|32|60|116|101|120| 116|62|128179|56499|60|47|116|101|120|116|62|10|60|47|99|117|115|116|111|109|101|114|62|10|