아래와 특수 문자를 입력하고 xml marshal 시 > < 로 자동 변환된다.
이것을 방지하기 위한 mashaller property 적용.
Response response = new Response();
termination.setValue("<![CDATA[<h1>test</h1>]]>");
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
import test.Response;
......
public String marshal(Response response)
{
if (response == null) return null;
try
{
StringWriter writer = new StringWriter();
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {
@Override
public void escape(char[] ac, int i, int j, boolean flag, Writer writer) throws IOException {
writer.write(ac, i, j);
}
});
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
marshaller.marshal(response, writer);
return writer.toString();
} catch(Exception e){}
return null;
}