반응형
반응형
반응형


아래와 특수 문자를 입력하고 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;

}



반응형
반응형


SAXParseException

<,&는 태그에만 사용가능하고 데이터로 사용 불가능 하다.

& => &amp;

반응형

'프로그래밍 > Java' 카테고리의 다른 글

ajax, jsp 에서 한글 깨짐 문제.  (1) 2009.04.23
서블릿 설정  (0) 2009.04.09
inputStream <-> String  (0) 2009.03.03
jstl 예제 - fn, fmt, foreach  (2) 2009.02.18
JSTL  (0) 2009.02.18

+ Recent posts