[jdom-interest] my last CDATA comment (I promise)

Kevin Regan kevinr at valicert.com
Fri Jun 2 11:42:37 PDT 2000


I think this subject has been beaten to death,
but I'll give it one more kick...

I think the following examples shows how much more
efficient (and aesthetically pleasing) the use
of CDATA sections can be.

Take, again, the HTML example where you want to
include a snippet of HTML code in your document.
(Please don't ask why, the programmer just wants to :-) ):

<HTML>
   <HEAD>
      <TITLE>The page that was hit.</TITLE>
   </HEAD>

   <BODY>
      <P>This is the page that was hit.</P>
   </BODY>
</HTML>

This can be output in two ways:

<HTML_PAGE><![CDATA[

<HTML>
   <HEAD>
      <TITLE>The page that was hit.</TITLE>
   </HEAD>

   <BODY>
      <P>This is the page that was hit.</P>
   </BODY>
</HTML>

]]></HTML_PAGE>

and:

<HTML_PAGE>

&lt;HTML&gt;
   &lt;HEAD&gt;
      &lt;TITLE&gt;The page that was hit.&lt;/TITLE&gt;
   &lt;HEAD&gt;

   &lt;BODY&gt;
      &lt;This is the page that was hit.&gt;
   &lt;/BODY&gt;
&lt;/HTML&gt;

</HTML_PAGE>

Now, you can imagine the huge efficiency hit that the
second output method causes.  We have to (1) create a
StringBuffer for the new string, (2) look at every character
in the output against 4 possible restricted characters
in a switch statement, (3) replace 1 character with 4
(or 5 for &amp;) characters for each escaped character,
(4) create a new String from the newly created StringBuffer
(oh, and (0) call the escape method in the first place).

For the first output we (1) do nothing special.  We just
output the string as is (wrapped in <![CDATA[...]]>).

As for which output is more human readable, I think
that is obvious.

--Kevin

p.s.  I don't think that arguments such as "use a different
      way of storing the page", or "you shouldn't be storing
      HTML pages", or "aesthetics does not matter" are
      invalid arguments.  You should not make judgements as
      to how someone wants to output well-formed XML, you
      should simply make it possible and let the user run
      with it.

 




More information about the jdom-interest mailing list