- Generic solution
Create a NoEscapeHandler implementing CharacterEscapeHandler (look for example at com.sun.xml.bind.marshaller.DumbEscapeHandler
import java.io.IOException;
import java.io.Writer;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
public class NoEscapeHandler implements CharacterEscapeHandler {
private NoEscapeHandler() {}
public static final CharacterEscapeHandler theInstance = new NoEscapeHandler();
public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
int limit = start+length;
for (int i = start; i < limit; i++) {
out.write(ch[i]);
}
}
}
then set the property for the marshaller
marshaller.setProperty("com.sun.xml.bind.characterEscapeHandler", NoEscapeHandler.theInstance);
Or use a DataWriter
StringWriter sw = new StringWriter();
DataWriter dw = new DataWriter(sw, "utf-8", NoEscapeHandler.theInstance);
Solution when using XmlStreamWriter and jaxb framgments
final XMLOutputFactory streamWriterFactory = XMLOutputFactory.newFactory();
streamWriterFactory.setProperty("escapeCharacters", false);
From here