XML Getting Started Tutorial: XML CDATA

Source: Internet
Author: User
Tags cdata contains reference return string xml cdata
xml| Tutorials | Getting Started

The text in all XML documents is parsed by the parser.

Only text in CDATA sections (CDATA section) is ignored by the parser.

Parsed Data

XML parsers typically parse all the text in an XML document.

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

<message> This text will also be parsed </message>

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

<name><first>Bill</first><last>Gates</last></name>

And the parser breaks it down into a child element like this:

<name>   <first>Bill</first>   <last>Gates</last></name>

Escape character

Illegal XML characters must be replaced with entity references (entity reference).

If you place a character in an XML document, such as "<," the document generates an error because the parser interprets it as the beginning of the new element. So you can't write like this:

<message>if Salary < 1000 then</message>

To avoid this error, you need to replace the character "<" with an entity reference, like this:

<message>if Salary < 1000 then</message>

There are 5 predefined entity references in XML:

< < Less than
> > Greater than
& & and number
' ' Elliptical number
" " Quotes

Note: Strictly speaking, only characters "<" and "&" in XML are illegal. ellipses, quotes, and greater-than numbers are legal, but it's a good practice to replace them with entity references.

Cdata

CDATA Everything inside will be ignored by the parser.

If your text contains a large number of "<" and "&" characters-as often happens in programming code-then this XML element can be defined as a CDATA part.

CDATA Extents start at "<![ Cdata[", ending with" ]]> ":

<script><! [Cdata[function matchwo (a,b) {if (a < b && a < 0) then   {return   1   }else   {return   0   } }]]></script>

In the above example, everything in the CDATA section will be ignored by the parser.

Comments about CDATA Sections:

CDATA Extents cannot contain the string "]]>", so the embedding of a CDATA section is not allowed.

You also need to make sure that there are no spaces or lines in the "]]>" string.



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: info-contact@alibabacloud.com 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.