XML DOM createCDATASection() Method
This article provides an in-depth explanation of the XML DOM createCDATASection() method.
Introduction to XML DOM
The XML Document Object Model (DOM) is a programming interface for XML documents. It represents the document as a tree structure where each node is an object representing a part of the document.
createCDATASection() Method
The createCDATASection() method creates a new CDATASection node with the specified data.
<script>
// Create a new CDATASection node
var cdata = document.createCDATASection("This is some CDATA content");
// Append the CDATASection node to an element
var parentElement = document.getElementById("myElement");
parentElement.appendChild(cdata);
</script>
The CDATASection node contains text that will be treated as character data and not parsed as XML markup.
Usage Example
The following example demonstrates how to use the createCDATASection() method to insert CDATA content into an existing XML document.
<!DOCTYPE html>
<html>
<body>
<h2>XML DOM createCDATASection() Example</h2>
<p>Click the button to insert CDATA content into the XML document.</p>
<button onclick="insertCDATA()">Insert CDATA</button>
<div id="demo"></div>
<script>
function insertCDATA() {
var xmlDoc = document.implementation.createDocument("", "", null);
var root = xmlDoc.createElement("root");
xmlDoc.appendChild(root);
// Create a new CDATASection node
var cdata = xmlDoc.createCDATASection("This is some CDATA content");
// Append the CDATASection node to the root element
root.appendChild(cdata);
// Display the XML document in the div element
document.getElementById("demo").innerHTML = xmlDoc.xml;
}
</script>
</body>
</html>
YouTip