开发者

Override declared encoding during unmarshalling with JAXB

开发者 https://www.devze.com 2023-04-02 22:00 出处:网络
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

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 unmar开发者_如何学编程shalling it with JAXB?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号