The CDATASection object is used to escape text that contains characters that would
otherwise be considered as markup. The main purpose of this is to hold such things as XML
fragments without having to escape all the delimiters, the only one recognized being the
'' character sequence that ends the CDATASection. CDATA sections cannot be nested.
The text of a CDATASection is stored in a Text node, and
may contain characters that need to be escaped outside of CDATA sections. Unlike
Text nodes, however, you cannot merge adjacent CDATA sections
using the Element object's
normalize method.
In the following example we use the 'staff.xml' file. The 'f_name' element of the first
'employee' consists of text which includes HTML <SPAN> tags with style information. In order
to prevent those tags being parsed by the XML parser, we include them in a CDATASection.
This allows them to pass through the parser intact and subsequently be applied as normal to
the text. Finally the code displays the nodeName,
nodeType and
data (note the affect of those <SPAN> tags)
properties for the CDATASection.
XML:
<staff>
<employee ssn="123456" pay="3">
<f_name><![CDATA[<span style="color:red">John</span>]]><f_name>
<l_name>Sullivan</l_name>
</employee>
<employee ssn="987654" pay="2">
<f_name>Mary</f_name>
<l_name>Lopez</l_name>
</employee>
</staff>
Code (JavaScript):
xml_doc = new ActiveXObject("Microsoft.XMLDOM");
xml_doc.async = false;
xml_doc.load("staff.xml");
elem = xml_doc.documentElement.firstChild.firstChild.firstChild;
document.write(elem.nodeName);
document.write("<br>" + elem.nodeType);
document.write("<br>" + elem.data);
Output:
#cdata-section
4
John
A CDATASection has no methods or properties of its own but inherits those of the
Text and Node objects. For details
of the values returned by the
nodeName,
nodeType and nodeValue
properties for a CDATASection, see the Node object.
denotes a Microsoft extension to the W3C DOM.
PROPERTIES
attributes Property
This is a read-only property that returns a
NamedNodeMap for nodes that can have
attributes.
Syntax: Node.attributes
baseName Property
This is a read-only property that returns the base name for a node.
Syntax: Node.baseName
childNodes Property
This is a read-only property containing a node list of all children for those elements that
can have them.
Syntax: Node.childNodes
data Property
This property contains the data for this node, depending on node type.
Syntax: CharacterData.data
dataType Property
This is a read-only property that specifies the data type for the node.
Syntax: Node.dataType
definition Property
This property returns the definition of the node in the DTD or schema.
Syntax: Node.definition
firstChild Property
This is a read-only property that returns the first child node of a node. If there is none,
it returns null.
Syntax: Node.firstChild
lastChild Property
This is a read-only property that returns the last child node of a node. If there is none,
it returns null.
Syntax: Node.lastChild