users@jaxb.java.net

Re: CDATA in marshalling output?

From: Malachi de AElfweald <malachi_at_EOTI.ORG>
Date: Tue, 13 May 2003 19:09:53 -0600

On Thu, 3 Apr 2003 08:07:19 -0800, Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM> wrote:
>I'm interested in learning more about how and why people wants to use
>CDATA (so that we can better understand what the problem is and how we
>can potentially solve it).
>
>regards,
>--
>Kohsuke KAWAGUCHI 408-276-7063 (x17063)
>Sun Microsystems kohsuke.kawaguchi_at_sun.com

I haven't been watching the list much lately, but found this question because I was looking for a way to use CDATA.

The difference between the two is quite simple. Text nodes are parsed (ie: escape things like ampersands), CDATAs are UNparsed character data (ie: NO escaping, can include ANY content except ']]>').

Why would one choose CDATA? I can think of two primary reasons. 1) If anyone is going to look at the actual XML (say, in IE) it is MUCH more readable without the escaping. especially if there is a lot of it. 2) legacy application integration.

My recommendation on how to implement this would be something along the lines of:

 <xsd:simpleType name="description">
  <xsd:restriction base="xsd:string">
   <xsd:annotation>
    <xsd:appinfo>
     <jaxb:impl type="CDATA"/>
    </xsd:appinfo>
   </xsd:annotation>
  </xsd:restriction>
 </xsd:simpleType>

or, even better:
 <xsd:element name="description" type="jaxb:CDATA" />


My 2 cents
Malachi