The problem sounds very easy, but I did not manage to get it working properly. I want to create XML, but one of the fields should be in CDATA format, as we are doing fulltext searching within so generated XMLs.
How can I tell JAXB to marschal the object while one of the field should be in CDATA block? What I tried is, I annotate the field with:
@XmlJavaTypeAdapter(value=CDATAAdapter.class)
protected String valueCDATA;
And the adapter:
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class CDATAAdapter extends XmlAdapter<String, String> {
@Override
public String marshal(String v) throws Exception {
return "<![CDATA[" + v + "]]>";
}
@Override
public String unmarshal(String v) throws Exception {
return v;
}
}
Thing is, it will escape it anyways. However if I use:
jaxbMarshaller.setProperty("com.sun.xml.internal.bind.characterEscapeHandler",
(CharacterEscapeHandler) (ch, start, length, isAttVal, writer1) -> writer1.write(ch, start, length));
But this will not escape any other field. So what is the solution for this?