File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes CDATA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "CDATA" Watch "CDATA" New topic


mary morris
Ranch Hand

Joined: Mar 16, 2002
Posts: 97
I have a xml file - it is machine generated. Basically it put everything into CDATA (sample below)- so text inside a CDATA section is ignored by the parser.
but does this not defeat the purpose of using XML?

- <![CDATA[ The slide show blah blah blah.
- <![CDATA[ 00B0D0E1E189087CDCD80201000009B4
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
The main purpose of using CDATA is suppose you have a text with lot of "<" and &&, then instead of writing < ( entity reference ) u can use CDATA so that parser will ignore that. say you have some line like this a<b && a>c && a<d . for parser < is a start of new element.
mary morris
Ranch Hand

Joined: Mar 16, 2002
Posts: 97
Thanks for your reply. So then if all text is inside CDATA tags what good is it to be in a XML file. Is it not defeating the purpose of a XML file?
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
If the text within a CDATA block would have been interpreted by the XML parser as a single text element anyway, we have lost nothing. If the text would have been interpreted as something else (two XML elements, invalid XML structure causing a parsing error), we have prevented a possibly fatal error.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
mary morris
Ranch Hand

Joined: Mar 16, 2002
Posts: 97
Thanks for your reply, so assuming it parses and is valid, what good is it to be a xml document. I have never seen a XML document where the whole file is enclosed in cdata sections.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
In preparing the scripts for on-line mock exams I use CDATA to enclose the text of questions, options and answers because the text may contain symbols that XML parsers recognize.
The XML tags provide the structure of the question - I really don't see the point of your objection.
mary morris
Ranch Hand

Joined: Mar 16, 2002
Posts: 97
I don't realy object, it was just that I didn't understand why one would do this. Although I do know the purpose of it, I and had never seen it used this way. But okay, would you call it good xml programming / standards?
Thanks for you reply.
I agree. Here's the link:
subject: CDATA
It's not a secret anymore!