If I'm building some XML and using <![CDATA[...]]>
, how do I encode data that may include a ]]>
substring? would it be ]]>
? I ask because I always thought CDATA content was literal content.
-
Duplicate http://stackoverflow.com/questions/538163/how-do-i-write-the-literal-inside-a-cdata-section-with-it-ending-the-section – tehvan Feb 12 '09 at 06:48
4 Answers
It's not terribly efficient, or easy to read, or anything, but an approach to this appears to be writing..
]]]]><
- 1,829
- 14
- 10
you can just use two cdata tags, the first one contains ]] and the second one contains >

- 41,826
- 12
- 125
- 142
CDATA is literal content in a sense, but of course there has to be some way to mark the end of a CDATA block and whatever you choose is going to have to be represented in your string somehow.
When I had to deal with XML documents containing CDATAs inside another CDATA block like you seem to have to, I gave up and encoded the whole thing as Base64. The other side then extracts the text and decodes it.
Base64 is handy because it has common support and/or simple algorithms to code and decode ASCII strings into it in many languages, in my case C# and Javascript in an ASP.NET app.

- 8,844
- 4
- 39
- 62
Shouldn't entities work?
like this: &93;&93;>
or, just one of them: ]]>
93 is for latin1, you would need unicode entities if sending data in utf-8.
As a sidenote, there are other characters which are dissallowed in a CDATA-block aswell, some weird quotations and control-flow characters.

- 24,126
- 6
- 49
- 75