I have an Object that is being marshalled to XML using JAXB. One element contains a String that includes quotes (\"). The resulting XML has " where t
I have just made my custom handler as a class like this:
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
public class XmlCharacterHandler implements CharacterEscapeHandler {
public void escape(char[] buf, int start, int len, boolean isAttValue,
Writer out) throws IOException {
StringWriter buffer = new StringWriter();
for (int i = start; i < start + len; i++) {
buffer.write(buf[i]);
}
String st = buffer.toString();
if (!st.contains("CDATA")) {
st = buffer.toString().replace("&", "&").replace("<", "<")
.replace(">", ">").replace("'", "'")
.replace("\"", """);
}
out.write(st);
System.out.println(st);
}
}
in the marshaller method simply call:
marshaller.setProperty(CharacterEscapeHandler.class.getName(),
new XmlCharacterHandler());
it works fine.