Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB Marshalling Unmarshalling with CDATA
    text
    copied!<p>i am trying to do marshaling with JAXB. </p> <p>my output is like</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;root&gt; &lt;name&gt;&amp;lt;![CDATA[&amp;lt;h1&amp;gt;kshitij&amp;lt;/h1&amp;gt;]]&amp;gt;&lt;/name&gt; &lt;surname&gt;&amp;lt;h1&amp;gt;solanki&amp;lt;/h1&amp;gt;&lt;/surname&gt; &lt;id&gt;&amp;lt;h1&amp;gt;1&amp;lt;/h1&amp;gt;&lt;/id&gt; &lt;/root&gt; </code></pre> <p>but i need output like</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;root&gt; &lt;name&gt;&lt;![CDATA[&lt;h1&gt;kshitij&lt;/h1&gt;]]&gt;&lt;/name&gt; &lt;surname&gt;&lt;![CDATA[&lt;h1&gt;solanki&lt;/h1&gt;]]&gt;&lt;/surname&gt; &lt;id&gt;&lt;![CDATA[0]]&gt;&lt;/id&gt; &lt;/root&gt; </code></pre> <p>I am using following code to do this. and if I uncomment code I get Property Binding Exception. Without it I can compile but I am not getting the exact required output.</p> <pre><code> 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 = "&lt;h1&gt;kshitij&lt;/h1&gt;"; String surname = "&lt;h1&gt;solanki&lt;/h1&gt;"; String id = "&lt;h1&gt;1&lt;/h1&gt;"; 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); } } } </code></pre> <p>and my bean lokks like</p> <pre><code> 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; } } </code></pre> <p>Adaptor Class</p> <pre><code>public class AdaptorCDATA extends XmlAdapter&lt;String, String&gt; { @Override public String marshal(String arg0) throws Exception { return "&lt;![CDATA[" + arg0 + "]]&gt;"; } @Override public String unmarshal(String arg0) throws Exception { return arg0; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload