|
If you have content in an XML document that contains many instances of <,
>, &, ", and ', it becomes tedious to have to escape each individual
instance.
Instead, you can place the content inside a CDATA section. Content
within a CDATA section is pure character data. The XML processor does not
try to interpret it in any way.
A CDATA section begins with:
<![CDATA[
and ends with:
]]>
For example, here's a JavaScript function placed inside a CDATA section:
<?xml version = "1.0" encoding = "utf-8"?>
<script>
<![CDATA[
// This function reverses the order of the children of Node n
function reverse(n)
{
var kids = n.childNodes; // Get the list of children
var numkids = kids.length; // Determine number of children
for (var i = numkids - 1; i >= 0; i--) // Loop through children backwards
{
var c = n.removeChild(kids[i]); // Remove a child
n.appendChild(c); // Put it back in its new position
}
}
]]>
</script>
and here's a CDATA section containing XML tags:
<?xml version="1.0" encoding="utf-8"?>
<description>
<book>
<title>The World is Flat</title>
<subtitle>A Brief History of the Twenty-First Century</subtitle>
<author>Thomas Friedman</author>
<publisher>Farrar, Straus and Giroux</publisher>
<date-published>April 30, 2006</date-published>
<isbn>0374292795</isbn>
<pages>593</pages>
<price currency="usd" type="full retail">30.00</price>
</book>
<![CDATA[
// Here is the above XML reproduced inside a CDATA section
<book>
<title>The World is Flat</title>
<subtitle>A Brief History of the Twenty-First Century</subtitle>
<author>Thomas Friedman</author>
<publisher>Farrar, Straus and Giroux</publisher>
<date-published>April 30, 2006</date-published>
<isbn>0374292795</isbn>
<pages>593</pages>
<price currency="usd" type="full retail">30.00</price>
</book>
]]>
</description>
Click here to see how
the above XML is rendered in your browser.
|