Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] Why won't XOM let you create a CDATA section?

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: "Lance Eason" <leason AT betweenmarkets.com>
  • To: <xom-interest AT lists.ibiblio.org>
  • Subject: [XOM-interest] Why won't XOM let you create a CDATA section?
  • Date: Tue, 22 Aug 2006 10:28:04 -0500

I'm trying to port over some existing JDOM based code to XOM and ran
into a bit of a snag. The code in question is wrapping a large block of
text in a CDATA section so I went to find the equivalent XOM API's and
discovered that XOM goes to some simply heroic efforts to prevent me
from ever, ever, ever creating a CDATA section. While internally it has
the ability to create one, the class and all the key methods are package
private and the package itself is sealed.

I know that in practical terms the character by character encoding of a
normal Text node accomplishes the same thing and in general I like the
fact that XOM takes responsibility for managing all the encoding issues
so that I don't have to but it would still be nice to be able to give it
a hint 'use a CDATA section here'. My real motivations have to do with
producing documents that are legible to humans (for debugging purposes)
and not breaking a number of existing unit tests but I can make
secondary arguments for encoding/decoding performance for large text
values and resulting document size when (as in my case) there are a
large number of characters that will end up encoded.

So why are CDATA sections so verboten in XOM?



P.S. By the way despite all the efforts to prevent it you can still
create a CDATA section using the public API you just have to play games
and rely on some existing behavior:

public class CDATAFactory
{
private static final Text PROTOTYPE; // this is really an instance
of CDATASection

static
{
Text temp = null;

try
{
// XOM preserves existing CDATA's so start with a doc that
has one
String docWithCDATA = "<root><![CDATA[prototype]]></root>";

Builder builder = new Builder();
Document document = builder.build(new
StringReader(docWithCDATA));

// grab the resulting CDATASection and keep it around as a
prototype
temp = (Text) document.getRootElement().getChild(0);
temp.detach();
}
catch (IOException e)
{
// not worried about IOExceptions just reading a string
}
catch (ParsingException e)
{
// already know this document is valid and will parse
}

PROTOTYPE = temp;
}

public static Text makeCDATASection(String value)
{
// use copy and setValue to get a brand new CDATA section
Text result = (Text) PROTOTYPE.copy();
result.setValue(value);
return result;
}
}





Archive powered by MHonArc 2.6.24.

Top of Page