• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to embed CDATA within another

 
Hank Smith
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic