Question

Under Windows 7 64 bit, I'm using JAXB to load an XML file endoded as UTF-8 without BOM (I can see that this is true by using an external sw, like Notepad++). When I open the file, I can see that the text is right. However, after unmarshalling I see that some chars are not properly loaded.

For instance SORDITÀ is loaded as SORDIT� (as reported by the Java console).

In my Java library I load the class DataContainer that contains the texts as follows:

File xmlFile = new File("C:/Data.xml"));
JAXBContext jaxbContext = JAXBContext.newInstance(DataContainer.class);
Unmarshaller um = jaxbContext.createUnmarshaller();
return (DataContainer) um.unmarshal(new FileReader(xmlFile));    

Am I missing such configuration? Is there any solution?

Était-ce utile?

La solution

Your problem ist that you pass a FileReader and a reader always needs an encoding. Because you haven't supplied one it is using the system encoding. In this case, Windows crap CP1252 and your two byte char is turned into two chars one byte each.

Rather pass a FileInputStream and let JAXB determine the proper encoding itself.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top