If you want to marshal Unicode code characters into XML with JaxB you should use the property JAXB_ENCODING with ISO-8859-1. Otherwise the output will result in some unreadable characters.
Applying this property will convert all Unicode characters in their escaped XML representation and it will also escape all control characters like greater.
Unicode characters and their escape sequence: http://www.fileformat.info/info/unicode/char/a.htm
Some Short, Self Contained, Correct (Compilable), Example:
public class TestJaxb {
public static void main(String[] args) throws JAXBException, IOException {
JAXBContext jc = JAXBContext.newInstance(MyElement.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
marshaller.marshal( new MyElement("ü UMLAUT > GREATER","\u03bb LAMBDA"), System.out );
marshaller.marshal( new MyElement("ü UMLAUT","\u03bb LAMBDA"), new FileOutputStream("jaxb.xml"));
}
@XmlRootElement static class MyElement {
@XmlElement public String x;
@XmlAttribute public String y;
MyElement() {}
MyElement(String x, String y) { this.x=x; this.y=y;}
}
}
Keine Kommentare:
Kommentar veröffentlichen