<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7650.28">
<TITLE>CDATA outputting issue</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/rtf format -->
<P><FONT SIZE=2 FACE="Arial">Hi everyone!</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">It's been a long time since my last post on this list - happy to be back ;-)</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">As a long time user of jdom, I'm currently struggling with an issue that I don't think I can find a workaround for. Check out the following source (easy and simple):</FONT></P>
<P><B><FONT COLOR="#7F0055" SIZE=2 FACE="Courier New">public</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New"></FONT><B> <FONT COLOR="#7F0055" SIZE=2 FACE="Courier New">class</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> CDATABug {</FONT>
</P>
<P><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT><B> <FONT COLOR="#7F0055" SIZE=2 FACE="Courier New">public</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New"></FONT><B> <FONT COLOR="#7F0055" SIZE=2 FACE="Courier New">static</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New"></FONT><B> <FONT COLOR="#7F0055" SIZE=2 FACE="Courier New">void</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> main(String[] args) {</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> Document doc =</FONT><B> <FONT COLOR="#7F0055" SIZE=2 FACE="Courier New">new</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> Document();</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> Element root =</FONT><B> <FONT COLOR="#7F0055" SIZE=2 FACE="Courier New">new</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> Element(</FONT><FONT COLOR="#2A00FF" SIZE=2 FACE="Courier New">"my_root"</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New">);</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> root.addContent(</FONT><B><FONT COLOR="#7F0055" SIZE=2 FACE="Courier New">new</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> Element(</FONT><FONT COLOR="#2A00FF" SIZE=2 FACE="Courier New">"my_nested"</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New">).addContent(</FONT><B><FONT COLOR="#7F0055" SIZE=2 FACE="Courier New">new</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> CDATA(</FONT><FONT COLOR="#2A00FF" SIZE=2 FACE="Courier New">""</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New">)));</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> doc.setRootElement(root);</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> Format format = Format.<I>getRawFormat</I>();</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> format.setTextMode(TextMode.</FONT><I><FONT COLOR="#0000C0" SIZE=2 FACE="Courier New">PRESERVE</FONT></I><FONT COLOR="#000000" SIZE=2 FACE="Courier New">);</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> XMLOutputter putter =</FONT><B> <FONT COLOR="#7F0055" SIZE=2 FACE="Courier New">new</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> XMLOutputter();</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> putter.setFormat(format);</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT><B> <FONT COLOR="#7F0055" SIZE=2 FACE="Courier New">try</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> {</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> putter.output(doc, System.</FONT><I><FONT COLOR="#0000C0" SIZE=2 FACE="Courier New">out</FONT></I><FONT COLOR="#000000" SIZE=2 FACE="Courier New">);</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> }</FONT><B> <FONT COLOR="#7F0055" SIZE=2 FACE="Courier New">catch</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> (IOException e) {</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> e.printStackTrace();</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> }</FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> }</FONT>
</P>
<P><FONT COLOR="#000000" SIZE=2 FACE="Courier New">}</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">IMHO, the expected output would be:</FONT>
</P>
<P><FONT COLOR="#000000" SIZE=2 FACE="Courier New"><?xml version="1.0" encoding="UTF-8"?></FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"><my_root><my_nested><![CDATA[]]></my_nested></my_root></FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">The output I get instead is (CDATA section completely omitted):</FONT>
</P>
<P><FONT COLOR="#000000" SIZE=2 FACE="Courier New"><?xml version="1.0" encoding="UTF-8"?></FONT>
<BR><FONT COLOR="#000000" SIZE=2 FACE="Courier New"><my_root><my_nested</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New">></FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"></my_nested></my_root></FONT>
</P>
<BR>
<P><FONT SIZE=2 FACE="Arial">Is this really the behaviour that one should expect given that I explicitly ask XMLOutputter to preserve all of my text? I'd argue that if I explicitly do that and additionally use a CDATA object instead of plain text, I should be seeing the CDATA section with empty content.</FONT></P>
<P><FONT SIZE=2 FACE="Arial">Could you give me a heads-up? There's no way around outputting the CDATA section as a client requires this behaviour and there's probably nothing I can do about them changing their specs. They explicitly asked for an empty CDATA section instead of an element with empty text content ..</FONT></P>
<BR>
<P><FONT SIZE=2 FACE="Arial">Thanks for your help!</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">Ben</FONT>
</P>
<BR>
</BODY>
</HTML>