aspose file tools*
The moose likes XML and Related Technologies and the fly likes how to embed CDATA within another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "how to embed CDATA within another" Watch "how to embed CDATA within another" New topic
Author

how to embed CDATA within another

Hank Smith
Greenhorn

Joined: Jan 23, 2007
Posts: 7
Hi,

Not sure if this question belongs in the Java forum or the XML forum.

I'm using JDOM in my Java app. I have an element that contains a CDATA section. Within that CDATA section is another XML document that itself has an element with a CDATA section.

What is the best way to handle the inner CDATA section, so the overall document is processed properly? It looks like the outside CDATA is "stopped" early in the processing by the closing tag of the inner CDATA.

I've tried using > and < characters in place of the greater-than and less-than characters.

I can't separate the XML into two separate documents; the format described above is mandated by another company's software to which I need to match.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

From the W3Schools tutorial about CDATA:
A CDATA section cannot contain the string "]]>", therefore, nested CDATA sections are not allowed.
My recommendation would be to not use CDATA sections at all. That other company can't be mandating CDATA if they are requesting XML, they must accept normally-escaped XML text.

However I can't imagine a design that puts an XML document in the middle of a larger text node. Is that really what you are dealing with?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
However I can't imagine a design that puts an XML document in the middle of a larger text node.


SOAP? The whole concept of envelope & payload is based on this. Ok, that's not the "middle" of a text node, it's the whole node.

Some years ago I ran into something where the envelope (proprietary, not SOAP) had to validate on some schema but the payload could be arbitrary well-formed XML. I think I left tried CDATA but then left the project and made somebody else figure it out.

BTW: I'm totally prepared to be wrong here - far from an XML expert.
[ February 22, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by Stan James:
SOAP? The whole concept of envelope & payload is based on this. Ok, that's not the "middle" of a text node, it's the whole node.
That's what I meant. Making a node contain an XML document, sure. Making a node contain an XML document surrounded by some other text, that's the idea that boggled me.

Anyway my feeling is that in Java, CDATA is a crutch for people who are avoiding using the standard Java classes to produce XML and are using the ordinary java.io classes. The standard Java classes, at least the ones that serialize data into XML, will do all that ugly escaping that makes an embedded XML document totally unreadable to the human eye while still producing well-formatted XML with the correct meaning.

And my opinion is that if you are doing something as complex as embedding XML documents inside other XML documents, you should really be using a JAXP Transformer to produce your XML. Or a similar serializer from Xerces or JDOM.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to embed CDATA within another