java - Xalan Transformer 转换破坏了 Unicode 6 星体字符

标签 java unicode xmlserializer xalan jaxp

角色1F48B是在Unicode 6.0中引入的

Unicode 6.0 support was introduced in Java 7 .

我无法让 Xalan 2.7.2 的序列化程序正确写入该字符;相反,它写的是��

下游,事情会变得丑陋:

org.xml.sax.SAXParseException; Character reference "&#55357" is an invalid XML character.
    at org.apache.xerces.parsers.AbstractSAXParser.parse

相比之下,Saxon 8.7 可以正确序列化它。

有人知道如何让 Xalan 正确书写吗?

这是显示问题的代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class SurrogatePairSerialisation {

    public static String TRANSFORMER_FACTORY_PROCESSOR_XALAN = "org.apache.xalan.processor.TransformerFactoryImpl";

    public static String TRANSFORMER_FACTORY_SAXON = "net.sf.saxon.TransformerFactoryImpl";

    public static TransformerFactory transformerFactory;

    static {
        System.setProperty("javax.xml.transform.TransformerFactory",
                TRANSFORMER_FACTORY_PROCESSOR_XALAN);
    //          TRANSFORMER_FACTORY_SAXON);

        transformerFactory = javax.xml.transform.TransformerFactory.newInstance();  
    }



public static void main(String[] args) throws Exception {

    // Verify using Java 7 or greater
    System.out.println(System.getProperty("java.vendor") );
    System.out.println( System.getProperty("java.version") ); 

    char[] chars = {55357, 56459};
    int codePoint = Character.codePointAt(chars, 0);

    // Verify its a valid code point
    System.out.println(Character.isValidCodePoint(codePoint));

    // Convert it to a string
    String astral = new String(Character.toChars(codePoint));

    // Show that we can write the string to a file
    FileOutputStream fos = new FileOutputStream(new File(System.getProperty("user.dir") + "/astral.txt"));
    fos.write(astral.getBytes("UTF-8"));
    fos.close(); // it is written as U+1F48B, as expected

    // Now show how it all falls apart with Xalan 
    // Create a DOM doc containing astral char
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder db = documentBuilderFactory.newDocumentBuilder();   
    Document doc = db.newDocument();
    Element foo = doc.createElement("foo");
    doc.appendChild(foo);
    foo.setTextContent(astral);


    // Write using Transformer transform 
    FileOutputStream fos2 = new FileOutputStream(new File(System.getProperty("user.dir") + "/astral.xml"));
    writeDocument(doc, fos2);
    fos2.close(); // Xalan writes �� but Saxon 8.7 is ok


}


protected static void writeDocument(Document document, OutputStream outputStream) throws Exception {
    Transformer serializer = transformerFactory.newTransformer();

    System.out.println(serializer.getClass().getName());

    serializer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
    serializer.setOutputProperty(javax.xml.transform.OutputKeys.METHOD, "xml");

    serializer.transform( new DOMSource(document) , new StreamResult(outputStream) );               
}


}

最佳答案

关于java - Xalan Transformer 转换破坏了 Unicode 6 星体字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32152196/

相关文章:

python - 使用python3查找表情符号的宽度

Python: latex 符号到unicode?

c# - 使用 XmlRootAttribute 将 XML 反序列化为 List<T>

java - 在 James 添加新用户

java - Apache Camel - 将消息路由到 JPA 端点 - 保持订单但根据类别 header 分成池

python - isdecimal 和 isdigit 之间的区别

c# - 无法将 xml 反序列化为 List<T>

c# - XmlSerializer 反序列化返回空数组

java - 如何在 solr schema.xml 中创建一个新的 FieldType

java - Flyway 不会在提供的架构中创建架构版本表