Apache CXF / JAXB unmarshaller将日文字符转换为?标记java

时间:2015-06-24 10:39:02

标签: java xml cxf

Apache CXF / JAXB不会解组日文字符。如果我们使用System.out.println打印xml,则输出正好如下所示。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee id="1470">
    <designation>Eng</designation>
    <name>マデュ</name>
    <salary>20000.0</salary>
</employee>

如果我们将相同的XML传递给CXF层,则转换如下。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee id="1470">
    <designation>Eng</designation>
    <name>???</name>
    <salary>20000.0</salary>
</employee>

如何解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试在StringWriter之上使用PrintWriter吗?

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

您可以尝试使用XMLEventWriter加载System.out吗?

        XMLEventFactory events = XMLEventFactory.newInstance();
        QName bar = new QName("urn:bar", "bar");
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
        XMLEventWriter writer = factory.createXMLEventWriter(System.out);


        JAXBContext pContext = JAXBContext.newInstance(target); 


        Marshaller marshaller = pContext.createMarshaller(); 
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); 
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 
        marshaller.marshal(pObject, writer); 

        writer.add(events.createStartDocument());
        writer.setDefaultNamespace("urn:bar");
        writer.add(events.createStartElement(bar, null, null));
        writer.add(events.createEndDocument());
        writer.flush();