Community
Participate
Working Groups
Build Identifier: 2.2.0 A Marshaller is instantiated like this: m = getContext().createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(Marshaller.JAXB_ENCODING, GlobalParams.DEFAULT_ENCODING); where GlobalParams.DEFAULT_ENCODING is "UTF-8". When altering objects and using the marshall process, the content of the XML file is garbled. Debugging the issue leads me to this function in the JAXBMarshaller class: public void marshal(Object object, File file) throws JAXBException { try { java.io.FileWriter writer = new java.io.FileWriter(file); try { marshal(object, writer); } finally { writer.close(); } } catch(Exception ex) { throw new MarshalException(ex); } } When inspecting the FileWriter instance I can see that the encoding is set to MS1252 (danish charset) which is an ISO8859 equivalent and is the system default encoding. I have set the System property file.encoding to "UTF-8". Please advice! Reproducible: Always Steps to Reproduce: 1.Choose a different system charset than the one used in the XML files 2.Construct a marshaller 3.Marshal
Using this works: OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(out), GlobalParams.DEFAULT_ENCODING); getMarshaller().marshal(o, writer);
The behaviour observed by Kasper is the expected behaviour. When marshalling to an instance of Writer the encoding is set on the Writer and cannot be changed by JAXB: import java.io.OutputStreamWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; public class MarshalDemo { public static void main(String[] args) throws Exception { Customer customer = new Customer(); customer.setName("Jane Doe"); Address address = new Address(); address.setStreet("123 A Street"); address.setCity("Any Town"); JAXBContext jc = JAXBContext.newInstance(Customer.class); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-16"); marshaller.marshal(customer, System.out); // Will marshal to UTF-16 OutputStreamWriter osw = new OutputStreamWriter(System.out); System.out.println(osw.getEncoding()); marshaller.marshal(customer, osw); // For me will marshal to Cp1252 marshaller.marshal(customer, new OutputStreamWriter(System.out, "UTF-16")); // Will marshal to UTF-16 } }
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink