Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML CDATA

XML CDATA


All text in an XML document is parsed by the parser.

Only text inside a CDATA section is ignored by the parser.


PCDATA - Parsed Character Data

An XML parser usually parses all text in an XML document.

When an XML element is parsed, the text between its tags is also parsed:

This text is also parsed

The parser does this because XML elements can contain other elements, just as in this example, where the element contains two other elements (first and last):

BillGates

And the parser breaks it down into child elements like this:

Bill

Gates

Parsed character data (PCDATA) is a term used for the text data parsed by an XML parser.


CDATA - (Unparsed) Character Data

The term CDATA refers to text data that should not be parsed by an XML parser.

Characters like "<" and "&" are illegal in XML elements.

"<" produces an error because the parser interprets the character as the start of a new element.

"&" produces an error because the parser interprets the character as the start of a character entity.

Some text, such as JavaScript code, contains many "<" or "&" characters. To avoid errors, you can define the script code as CDATA.

Everything inside a CDATA section is ignored by the parser.

A CDATA section starts with the string "" and ends with the string:

In the example above, the parser ignores everything inside the CDATA section.

Notes about CDATA sections:

A CDATA section cannot contain the string "]]>". Nested CDATA sections are not allowed either.

The "]]>" that marks the end of a CDATA section cannot contain spaces or line breaks.