Override declared encoding during unmarshalling with JAXB

我与影子孤独终老i 提交于 2019-12-21 07:59:07

问题


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?


回答1:


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();
}

For More Information

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


来源:https://stackoverflow.com/questions/7332329/override-declared-encoding-during-unmarshalling-with-jaxb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!