猿问

<![CDATA[]>在XML中意味着什么?

<![CDATA[]>在XML中意味着什么?

我经常觉得这很奇怪CDATA标记入XML档案:

<![CDATA[some stuff]]>

我注意到CDATA标签总是出现在开头,然后跟着一些东西。

但有时它被使用,有时却不是。我想是为了标记some stuff是在之后插入的“数据”。但是什么样的数据是some stuff?我用XML标记写的东西难道不是某种数据吗?


繁花不似锦
浏览 3145回答 3
3回答

慕村225694

CDATA代表字符数据这意味着这些字符串之间的数据包括能被解释为XML标记,但不应该解释为XML标记。CDATA与评论之间的主要区别是:如理查德指出,CDATA仍然是文档的一部分,而注释则不是。在CDATA中,不能包含字符串。]]>&nbsp;(CDEnd),而在评论中--无效.参数实体注释内部不能识别引用。这意味着从一个格式良好的文档中获得这三个XML片段:<!ENTITY&nbsp;MyParamEntity&nbsp;"Has&nbsp;been&nbsp;expanded"><!-- Within&nbsp;this&nbsp;comment&nbsp;I&nbsp;can&nbsp;use&nbsp;]]> and&nbsp;other&nbsp;reserved&nbsp;characters&nbsp;like&nbsp;< &,&nbsp;',&nbsp;and&nbsp;",&nbsp;but&nbsp;%MyParamEntity;&nbsp;will&nbsp;not&nbsp;be&nbsp;expanded (if&nbsp;I&nbsp;retrieve&nbsp;the&nbsp;text&nbsp;of&nbsp;this&nbsp;node&nbsp;it&nbsp;will&nbsp;contain %MyParamEntity;&nbsp;and&nbsp;not&nbsp;"Has&nbsp;been&nbsp;expanded") and&nbsp;I&nbsp;can't&nbsp;place&nbsp;two&nbsp;dashes&nbsp;next&nbsp;to&nbsp;each&nbsp;other. --><![CDATA[ Within&nbsp;this&nbsp;Character&nbsp;Data&nbsp;block&nbsp;I&nbsp;can use&nbsp;double&nbsp;dashes&nbsp;as&nbsp;much&nbsp;as&nbsp;I&nbsp;want&nbsp;(along&nbsp;with&nbsp;<,&nbsp;&,&nbsp;',&nbsp;and&nbsp;") *and*&nbsp;%MyParamEntity;&nbsp;will&nbsp;be&nbsp;expanded&nbsp;to&nbsp;the&nbsp;text "Has&nbsp;been&nbsp;expanded"&nbsp;...&nbsp;however,&nbsp;I&nbsp;can't&nbsp;use the&nbsp;CEND&nbsp;sequence.&nbsp;If&nbsp;I&nbsp;need&nbsp;to&nbsp;use&nbsp;CEND&nbsp;I&nbsp;must&nbsp;escape&nbsp;one&nbsp;of&nbsp;the brackets&nbsp;or&nbsp;the&nbsp;greater-than&nbsp;sign&nbsp;using&nbsp;concatenated&nbsp;CDATA&nbsp;sections. ]]><description>An&nbsp;example&nbsp;of&nbsp;escaped&nbsp;CENDs</description><!--&nbsp;This&nbsp;text&nbsp;contains&nbsp;a&nbsp;CEND&nbsp;]]>&nbsp;--><!--&nbsp;In&nbsp;this&nbsp;first&nbsp;case&nbsp;we&nbsp;put&nbsp;the&nbsp;]]&nbsp;at&nbsp;the&nbsp;end&nbsp; of&nbsp;the&nbsp;first&nbsp;CDATA&nbsp;block &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;the&nbsp;>&nbsp;in&nbsp;the&nbsp;second&nbsp;CDATA&nbsp;block&nbsp;--><data><![CDATA[This&nbsp;text&nbsp;contains&nbsp;a&nbsp;CEND&nbsp;]]]]><![CDATA[>]]></data><!--&nbsp;In&nbsp;this&nbsp;second&nbsp;case&nbsp;we &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;put&nbsp;a&nbsp;]&nbsp;at&nbsp;the&nbsp;end&nbsp;of&nbsp;the&nbsp;first&nbsp;CDATA&nbsp;block &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;the&nbsp;]>&nbsp;in&nbsp;the&nbsp;second&nbsp;CDATA&nbsp;block&nbsp;--><alternative><![CDATA[This&nbsp;text&nbsp;contains&nbsp;a&nbsp;CEND&nbsp;]]]><![CDATA[]>]]></alternative>

开心每一天1111

一个很大的用例:您的XML包含一个程序,作为数据(例如,Java的网页教程)。在这种情况下,您的数据包含大量字符,其中包括‘&’和‘<’,但是这些字符不应该是XML。比较:<example-code>while&nbsp;(x&nbsp;&lt;&nbsp;len&nbsp;&amp;&amp;&nbsp;!done)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;print(&nbsp;&quot;Still&nbsp;working,&nbsp;&apos;zzz&apos;.&quot;&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;++x; &nbsp;&nbsp;&nbsp;&nbsp;}</example-code>带着<example-code><![CDATA[ while&nbsp;(x&nbsp;<&nbsp;len&nbsp;&&&nbsp;!done)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;print(&nbsp;"Still&nbsp;working,&nbsp;'zzzz'."&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;++x; &nbsp;&nbsp;&nbsp;&nbsp;} ]]></example-code>特别是如果您正在从文件中复制/粘贴这段代码(或者将其包括在预处理器中),那么在XML文件中只包含您想要的字符是很好的,w/o将它们与XML标记/属性混为一谈。正如@paary所提到的,其他常见用途包括嵌入包含符号的URL时。最后,即使数据只包含几个特殊字符,但数据很长(比如说,一章的文本),在编辑XML文件时不必对这几个实体进行编码也很好。(我怀疑所有与评论的比较都有点误导/毫无帮助。)
随时随地看视频慕课网APP
我要回答