如何使用JAXB Marshaller

时间:2018-02-09 00:27:26

标签: java spring jaxb escaping marshalling

我正在使用JAXB Marshaller,我想要转义字符。例如双引号(")显示为�。我该如何处理这些?

我通过关注其他类似帖子来设置属性,但没有解决问题。有谁可以在这里建议?任何帮助表示赞赏。

代码:

JAXBContext jc = JAXBContext.newInstance(object.getClass().getPackage().getName());

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty("jaxb.encoding", "Unicode");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {
            @Override
            public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
                out.write( ch, start, length ); 
            }
        }); 
        marshaller.marshal(object, stringWriter);

1 个答案:

答案 0 :(得分:2)

编组应该能够使用编码

marshaller.setProperty("jaxb.encoding", "UTF-8");
相关问题