Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to marshall a string using JAXB that sometimes contains XML content and sometimes does not?
    primarykey
    data
    text
    <p>Consider this example - </p> <p>I have a class called Report that has a field of type Message. The Message class has a field called "body" which is a string. "body" can be any string, <strong>but sometimes it contains properly formatted XML content</strong>. How can I ensure that when the "body" contains XML content, the serialization takes the form of an XML structure rather than what it gives at present? </p> <p>Here is the code with the output - </p> <p><strong>Report</strong> class</p> <pre><code>import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "Report") @XmlType(propOrder = { "message"}) public class Report { private Message message; public Message getMessage() { return message; } public void setMessage(Message m) { message = m; } } </code></pre> <p><strong>Message</strong> class</p> <pre><code>import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; @XmlType(propOrder = { "body" }) public class Message { private String body; public String getBody() { return body; } @XmlElement public void setBody(String body) { this.body = body; } } </code></pre> <p>Main</p> <pre><code>import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class SerializationTest { public static void main(String args[]) throws Exception { JAXBContext jaxbContext = JAXBContext.newInstance(Report.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); Report report = new Report(); Message message = new Message(); message.setBody("Sample report message."); report.setMessage(message); jaxbMarshaller.marshal(report, System.out); message.setBody("&lt;rootTag&gt;&lt;body&gt;All systems online.&lt;/body&gt;&lt;/rootTag&gt;"); report.setMessage(message); jaxbMarshaller.marshal(report, System.out); } } </code></pre> <p>The output is as follows - </p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;Report&gt; &lt;message&gt; &lt;body&gt;Sample report message.&lt;/body&gt; &lt;/message&gt; &lt;/Report&gt; &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;Report&gt; &lt;message&gt; &lt;body&gt;&amp;lt;rootTag&amp;gt;&amp;lt;body&amp;gt;All systems online.&amp;lt;/body&amp;gt;&amp;lt;/rootTag&amp;gt;&lt;/body&gt; &lt;/message&gt; &lt;/Report&gt; </code></pre> <p>As you can see in the above output, for the second instance of "body", the serialization produced</p> <pre><code> &lt;body&gt;&amp;lt;rootTag&amp;gt;&amp;lt;body&amp;gt;All systems online.&amp;lt;/body&amp;gt;&amp;lt;/rootTag&amp;gt;&lt;/body&gt; </code></pre> <p>instead of</p> <pre><code>&lt;body&gt;&lt;rootTag&gt;&lt;body&gt;All systems online.&lt;/body&gt;&lt;/rootTag&gt;&lt;/body&gt; </code></pre> <p>How to solve this problem?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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