有没有办法在不使用EclipseLink API 的情况下在JAXB中使用 CDATA?
JAXB课程:
@XmlRootElement(name = "Documentation")
@XmlAccessorType(XmlAccessType.FIELD)
public class Documentation {
@XmlJavaTypeAdapter(AdaptorCDATA.class)
@XmlValue
protected String value; //setter & getter implemeted
}
Marshaller Property :必需的eclipselink api org.eclipse.persistence.oxm.CharacterEscapeHandler
marshaller.setProperty(CharacterEscapeHandler.class.getName(), new org.eclipse.persistence.oxm.CharacterEscapeHandler() { // property required for CDATA
@Override
public void escape(char[] ac, int i, int j, boolean flag,
Writer writer) throws IOException {
writer.write( ac, i, j ); }
});
适配器类:
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class AdaptorCDATA extends XmlAdapter<String, String> {
@Override
public String marshal(String arg0) throws Exception {
return "<![CDATA[" + arg0 + "]]>";
}
@Override
public String unmarshal(String arg0) throws Exception {
return arg0;
}
}