Use of CDATA in XML

In this article I am going to tell about XML CDATA.
  • 2293

Definition

XML CDATA (Character Data) is declare because of we want come data will ignored by the parser. All we know that all text in an XML document will be parsed by the parser.

Characters like "<" and "&" are illegal in XML elements. "<" will generate an error because the parser interprets it as the start of a new element. "&" will generate an error because the parser interprets it as the start of an character entity. Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script code can be defined as CDATA.

Example

In this example, we have a block of JavaScript code inside an XHTML document. If you've been involved in web development, you'll probably know how common JavaScript is on the web. You might also know that any block of JavaScript code could contain all sorts of potentially problematic characters.

<?xml version="1.0"?>
<!DOCTYPE html>
<html>
<head>
   <script type="text/javascript">
        <![CDATA[
          function mcn(p,q)
           {
              if (p < q && p < 0) then
                {
                   return 1;
                }
              else
               {
                   return 0;
               }
          }
      ]]>
   </script>
</head>
<body></body>
</html>

You may read more articles about XML here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.