在使用JAXB解组时重写声明的编码 [英] Override declared encoding during unmarshalling with JAXB

查看:74
本文介绍了在使用JAXB解组时重写声明的编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,其中包含编码:<?xml version =1.0encoding =ISO-8859-15?> 但是真正的文件以UTF-8编码。有没有办法在使用JAXB解组时覆盖XML文件中声明的编码?

I have an XML file with its encoding set within it: <?xml version="1.0" encoding="ISO-8859-15"?> but really file is encoded in UTF-8. Is there a way to override encoding declared in XML file when unmarshalling it with JAXB?

推荐答案

您可以从 java.io.Reader 解组内容为了提供实际的编码:

You can unmarshal the content from a java.io.Reader in order to supply the actual encoding:

Unmarshaller unmarshaller = jc.createUnmarshaller();
InputStream inputStream = new FileInputStream("input.xml");
Reader reader = new InputStreamReader(inputStream, "UTF-8");
try {
    Address address = (Address) unmarshaller.unmarshal(reader);
} finally  {
    reader.close();
}

更多信息

  • http://blog.bdoughan.com/2011/08/jaxb-and-java-io-files-streams-readers.html

这篇关于在使用JAXB解组时重写声明的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆