The role of CDATA in XML files

Source: Internet
Author: User
Tags xml cdata xml parser

When manipulating an XML file, allowing the user to enter content such as "<", ">", "/", "", and so on, when the XML is generated, destroys the XML structure and interrupts the data.

All text in an XML document is parsed by the parser, which is used in XML CDATA, and only the text within the CDATA part is ignored by the parser.

1. Parsing text
The XML parser typically processes all the text in an XML document.

When the XML element is parsed, the text inside the XML element is also parsed:<message>this text is also parsed</message>.

The XML parser does this because the XML element may also contain other elements, as in the following example, the name element contains the first and last two elements inside:





The parser will assume that the above code is this:


2. Escape character
An illegal XML character must be replaced with the corresponding entity.

If you use a character like "<" in an XML document, the parser will get an error because the parser will assume that this is the beginning of a new element. Therefore, you should not write the code as follows:

<message>if Salary < Then</message>

To avoid this situation, the character "<" must be converted to an entity, as follows:

< Message>if salary < then</message>

Here are five pre-defined entities in the XML document:

< < less sign
> > Greater than Sign
& & and
&apos; ' Single quotation mark
"" Double quotation marks

The entity must begin with the symbol "&", with the symbol ";" End.
Note: Only the "<" character and the "&" character are strictly forbidden for XML. The rest is legal, and the use of entities is a good habit in order to reduce errors.

3.CDATA Parts
All content inside CDATA is ignored by the parser.

If the text contains a lot of "<" characters and "&" characters-just like the program code-it's best to put them all in a CDATA widget.

A CDATA widget with "<! The [cdata[] tag begins with the "]]>" Mark:

<! [cdata[
function Matchwo (A, b) {
if (A < b && a < 0) then {
Return 1
} else {
return 0

In the previous example, all the text between the CDATA parts is ignored by the parser.

CDATA Considerations:
CDATA Parts can no longer contain CDATA parts (not nested). If the CDATA part contains the character "]]>" or "<! [cdata[], will be very likely to make mistakes oh.

Also note that there is no space or line break between the string "]]>".

The role of CDATA in XML files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.