Java Xml Transformation转义代表补充字符的代理单元

时间:2016-12-26 23:22:26

标签: java xml surrogate-pairs

我正在servlet Tomcat 8.0的容器中执行Web应用程序。在请求中,我尝试使用下面的代码将输入数据转换为XML。第一个输入数据字符是一个unicode补充字符U + 16980,表示为字符对\ ud81a \ udd80,第二个字符是另一个补充字符U + 16990,表示为字符对\ ud81a \ udd90。

    String text = "    �  �";
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.newDocument();

    Element root = document.createElement("root");
    document.appendChild(root);
    Element node = document.createElement("sofa");

    node.appendChild(document.createTextNode(text));

    root.appendChild(node);

    Source xmlSource = new DOMSource(document);

    // create StreamResult for transformation result
    javax.xml.transform.Result result = new StreamResult(new FileOutputStream("text.xml"));

    // create TransformerFactory
    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    // create Transformer for transformation
    Transformer transformer = transformerFactory.newTransformer();

    // transform and deliver content to client
    transformer.transform(xmlSource, result);

我期待:<root><sofa>&#92544; &#92560; � �</sofa></root>

但我得到:<root><sofa>&#55322;&#56704; &#55322;&#56720; � �</sofa> </root>

0 个答案:

没有答案