JAXB Marshalling Unmarshalling con CDATA

sto provando a fare il marshalling con JAXB.

la mia uscita è come

  <![CDATA[<h1>kshitij</h1>]]> <h1>solanki</h1> <h1>1</h1>  

ma ho bisogno di uscita come

   <![CDATA[

kshitij

]]> <![CDATA[

solanki

]]>

Sto usando il seguente codice per fare questo. e se ho decommentato codice ottengo Exception Binding di proprietà. Senza di esso posso compilare, ma non sto ottenendo l’output richiesto esatto.

  package com.ksh.templates; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import com.sun.xml.bind.marshaller.CharacterEscapeHandler; public class MainCDATA { public static void main(String args[]) { try { String name = "

kshitij

"; String surname = "

solanki

"; String id = "

1

"; TestingCDATA cdata = new TestingCDATA(); cdata.setId(id); cdata.setName(name); cdata.setSurname(surname); JAXBContext jaxbContext = JAXBContext.newInstance(TestingCDATA.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() { public void escape(char[] ac, int i, int j, boolean flag, Writer writer) throws IOException { writer.write( ac, i, j ); } }); StringWriter stringWriter = new StringWriter(); marshaller.marshal(cdata, stringWriter); System.out.println(stringWriter.toString()); } catch (Exception e) { System.out.println(e); } } }

e il mio fagiolo mi piace

  package com.ksh.templates; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import com.sun.xml.txw2.annotation.XmlCDATA; @XmlRootElement(name = "root") @XmlAccessorType(XmlAccessType.FIELD) public class TestingCDATA { @XmlElement @XmlJavaTypeAdapter(value = AdaptorCDATA.class) private String name; @XmlElement @XmlJavaTypeAdapter(value = AdaptorCDATA.class) private String surname; @XmlCDATA public String getName() { return name; } @XmlCDATA public void setName(String name) { this.name = name; } @XmlCDATA public String getSurname() { return surname; } @XmlCDATA public void setSurname(String surname) { this.surname = surname; } } 

Classe adattatore

 public class AdaptorCDATA extends XmlAdapter { @Override public String marshal(String arg0) throws Exception { return ""; } @Override public String unmarshal(String arg0) throws Exception { return arg0; } }