我经常在XML文件中发现这个奇怪的CDATA标签:
<![CDATA[some stuff]]>
我观察到这个CDATA标记总是出现在开头,然后跟着一些东西。
但有时用,有时不用。我假设这是为了标记一些东西是“数据”,将被插入之后。但是什么是数据呢?我用XML标记写的东西不是某种数据吗?
我经常在XML文件中发现这个奇怪的CDATA标签:
<![CDATA[some stuff]]>
我观察到这个CDATA标记总是出现在开头,然后跟着一些东西。
但有时用,有时不用。我假设这是为了标记一些东西是“数据”,将被插入之后。但是什么是数据呢?我用XML标记写的东西不是某种数据吗?
当前回答
Cdata是您可能希望传递给xml解析器但仍然不解释为xml的数据。
Say for eg :- You have an xml which has encapsulates question/answer object . Such open fields can have any data which does not strictly fall under basic data type or xml defined custom data types. Like --Is this a correct tag for xml comment ? .-- You may have a requirement to pass it as it is without being interpreted by the xml parser as another child element. Here Cdata comes to your rescue . By declaring as Cdata you are telling the parser don't treat the data wrapped as an xml (though it may look like one )
其他回答
其中包含的数据不会被解析为XML,因此不需要是有效的XML,或者可以包含看似XML但实际上不是的元素。
CDATA代表字符数据。您可以使用它来转义某些字符,否则这些字符将被视为常规XML。其中的数据将不会被解析。 例如,如果你想传递一个包含&的URL,你可以使用CDATA来实现。否则,您将得到一个错误,因为它将被解析为常规XML。
CDATA节是“元素内容的一段,它被标记为仅供解析器解释为字符数据,而不是标记”。
在语法上,它的行为类似于注释:
<exampleOfAComment>
<!--
Since this is a comment
I can use all sorts of reserved characters
like > < " and &
or write things like
<foo></bar>
but my document is still well-formed!
-->
</exampleOfAComment>
... 但它仍然是文件的一部分:
<exampleOfACDATA>
<![CDATA[
Since this is a CDATA section
I can use all sorts of reserved characters
like > < " and &
or write things like
<foo></bar>
but my document is still well formed!
]]>
</exampleOfACDATA>
尝试将以下内容保存为.xhtml文件(不是.html),并使用FireFox(不是Internet Explorer)打开它,以查看注释和CDATA部分之间的差异;当你在浏览器中查看文档时,注释不会出现,而CDATA部分将:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >
<head>
<title>CDATA Example</title>
</head>
<body>
<h2>Using a Comment</h2>
<div id="commentExample">
<!--
You won't see this in the document
and can use reserved characters like
< > & "
-->
</div>
<h2>Using a CDATA Section</h2>
<div id="cdataExample">
<![CDATA[
You will see this in the document
and can use reserved characters like
< > & "
]]>
</div>
</body>
</html>
使用CDATA节需要注意的一点是,它们没有编码,因此无法在其中包含字符串]]>。任何包含]]>的字符数据都必须-据我所知-是一个文本节点。同样地,从DOM操作的角度来看,你不能创建一个包含[]>的CDATA节:
var myEl = xmlDoc.getElementById("cdata-wrapper");
myEl.appendChild(xmlDoc.createCDATASection("This section cannot contain ]]>"));
这个DOM操作代码要么抛出一个异常(在Firefox中),要么导致一个结构糟糕的XML文档:http://jsfiddle.net/9NNHA/
它转义一个不能像往常一样传递给XML的字符串:
例子:
字符串中包含“&”。
你不能:
<FL val="Company Name">Dolce & Gabbana</FL>
因此,您必须使用CDATA:
<FL val="Company Name"> <![CDATA["Dolce & Gabbana"]]> </FL>
Cdata是您可能希望传递给xml解析器但仍然不解释为xml的数据。
Say for eg :- You have an xml which has encapsulates question/answer object . Such open fields can have any data which does not strictly fall under basic data type or xml defined custom data types. Like --Is this a correct tag for xml comment ? .-- You may have a requirement to pass it as it is without being interpreted by the xml parser as another child element. Here Cdata comes to your rescue . By declaring as Cdata you are telling the parser don't treat the data wrapped as an xml (though it may look like one )