我有一个XML文件及其编码集:<?xml version="1.0" encoding="ISO-8859-15"?>
但实际上文件是用UTF-8编码的。有没有办法在使用JAXB解组时覆盖XML文件中声明的编码?
答案 0 :(得分:19)
您可以从java.io.Reader
解组内容以提供实际编码:
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();
}
了解更多信息