为什么在中包含XML标记数据

时间:2022-10-27 10:36:39

I have seen in many XML file has the data inside this <![CDATA[ ]]>. What is the use of this?

我在很多XML文件中看到过这个 里面的数据。有什么用?

4 个解决方案

#1


5  

From Wikipedia

A CDATA section starts with the following sequence:

CDATA部分以以下顺序开头:

<![CDATA[

and ends with the first occurrence of the sequence:

并以第一次出现的序列结束:

]]>

All characters enclosed between these two sequences are interpreted as characters, not markup or entity references. For example, in a line like this:

包含在这两个序列之间的所有字符都被解释为字符,而不是标记或实体引用。例如,在这样的行中:

<sender>John Smith</sender>

the opening and closing "sender" tags are interpreted as markup. However, if written like this:

打开和关闭“sender”标签被解释为标记。但是,如果这样写:

<![CDATA[<sender>John Smith</sender>]]>

then the code is interpreted the same as if it had been written like this:

那么代码的解释就像它写成这样:

&lt;sender&gt;John Smith&lt;/sender&gt;

Check out the Wiki page for more details.

查看Wiki页面了解更多详情。

#2


3  

See CDATA at Wikipedia:

请参阅*的CDATA:

In an XML document or external parsed entity, a CDATA section is a section of element content that is marked for the parser to interpret as only character data, not markup.

在XML文档或外部解析的实体中,CDATA部分是元素内容的一部分,标记为解析器仅解释为字符数据,而不是标记。

That means the parser will not interpret any data contained in <![CDATA[ ]]>.

这意味着解析器不会解释 中包含的任何数据。

Lets say you want to store HTML as text in an XML document. This is wrong:

假设您希望将HTML作为文本存储在XML文档中。这是错的:

<root>
    <myhtml>
        <div><b>Foo bar</b></div>
    </myhtml>
</root>

The parser would try to parse <div><b>Foo bar</b></div> and create DOM elements. It would work in this example, but not for documents that conform to an DTD.

解析器将尝试解析

Foo bar 并创建DOM元素。它适用于此示例,但不适用于符合DTD的文档。

Instead, we want this:

相反,我们想要这个:

<root>
    <myhtml>
        <![CDATA[ <div><b>Foo bar</b></div> ]]>
    </myhtml>
</root>

and <div><b>Foo bar</b></div> will become the content of a text node.

Foo bar 将成为文本节点的内容。

#3


0  

It allows literal text, including most xml "special characters". it's not part of the "outer" xml data but just text.

它允许文字文本,包括大多数xml“特殊字符”。它不是“外部”xml数据的一部分,而只是文本。

#4


0  

Anything inside a CDATA section is ignored by the parser.

解析器会忽略CDATA部分中的任何内容。

For instance image that you want to send javascript inside an xml document, since it will most probably contain special chars (<,>,&, etc) the xml parser will produce errors.

例如,您想要在xml文档中发送javascript的图像,因为它很可能包含特殊字符(<,>,&等),xml解析器将产生错误。

With this tag the parser will not parse it and no errors will be produced.

使用此标记,解析器不会解析它,也不会产生错误。

#1


5  

From Wikipedia

A CDATA section starts with the following sequence:

CDATA部分以以下顺序开头:

<![CDATA[

and ends with the first occurrence of the sequence:

并以第一次出现的序列结束:

]]>

All characters enclosed between these two sequences are interpreted as characters, not markup or entity references. For example, in a line like this:

包含在这两个序列之间的所有字符都被解释为字符,而不是标记或实体引用。例如,在这样的行中:

<sender>John Smith</sender>

the opening and closing "sender" tags are interpreted as markup. However, if written like this:

打开和关闭“sender”标签被解释为标记。但是,如果这样写:

<![CDATA[<sender>John Smith</sender>]]>

then the code is interpreted the same as if it had been written like this:

那么代码的解释就像它写成这样:

&lt;sender&gt;John Smith&lt;/sender&gt;

Check out the Wiki page for more details.

查看Wiki页面了解更多详情。

#2


3  

See CDATA at Wikipedia:

请参阅*的CDATA:

In an XML document or external parsed entity, a CDATA section is a section of element content that is marked for the parser to interpret as only character data, not markup.

在XML文档或外部解析的实体中,CDATA部分是元素内容的一部分,标记为解析器仅解释为字符数据,而不是标记。

That means the parser will not interpret any data contained in <![CDATA[ ]]>.

这意味着解析器不会解释 中包含的任何数据。

Lets say you want to store HTML as text in an XML document. This is wrong:

假设您希望将HTML作为文本存储在XML文档中。这是错的:

<root>
    <myhtml>
        <div><b>Foo bar</b></div>
    </myhtml>
</root>

The parser would try to parse <div><b>Foo bar</b></div> and create DOM elements. It would work in this example, but not for documents that conform to an DTD.

解析器将尝试解析

Foo bar 并创建DOM元素。它适用于此示例,但不适用于符合DTD的文档。

Instead, we want this:

相反,我们想要这个:

<root>
    <myhtml>
        <![CDATA[ <div><b>Foo bar</b></div> ]]>
    </myhtml>
</root>

and <div><b>Foo bar</b></div> will become the content of a text node.

Foo bar 将成为文本节点的内容。

#3


0  

It allows literal text, including most xml "special characters". it's not part of the "outer" xml data but just text.

它允许文字文本,包括大多数xml“特殊字符”。它不是“外部”xml数据的一部分,而只是文本。

#4


0  

Anything inside a CDATA section is ignored by the parser.

解析器会忽略CDATA部分中的任何内容。

For instance image that you want to send javascript inside an xml document, since it will most probably contain special chars (<,>,&, etc) the xml parser will produce errors.

例如,您想要在xml文档中发送javascript的图像,因为它很可能包含特殊字符(<,>,&等),xml解析器将产生错误。

With this tag the parser will not parse it and no errors will be produced.

使用此标记,解析器不会解析它,也不会产生错误。