What does <![CDATA[]]> in XML mean?

前端 未结 13 2190
独厮守ぢ
独厮守ぢ 2020-11-21 07:52

I often find this strange CDATA tag in XML files:


I have observed that this CD

相关标签:
13条回答
  • 2020-11-21 07:57

    I once had to use CDATA when my xml element needed to store HTML code. Something like

    <codearea>
      <![CDATA[ 
      <div> <p> my para </p> </div> 
      ]]>
    </codearea>
    

    So CDATA means it will ignore any character which could otherwise be interpreted as XML tag like < and > etc.

    0 讨论(0)
  • 2020-11-21 07:58

    It's used to contain data which could otherwise be seen as xml because it contains certain characters.

    This way the data inside will be displayed, but not interpreted.

    0 讨论(0)
  • 2020-11-21 08:00

    CDATA stands for Character Data. You can use this to escape some characters which otherwise will be treated as regular XML. The data inside this will not be parsed. For example, if you want to pass a URL that contains & in it, you can use CDATA to do it. Otherwise, you will get an error as it will be parsed as regular XML.

    0 讨论(0)
  • 2020-11-21 08:01

    CDATA stands for Character Data and it means that the data in between these strings includes data that could be interpreted as XML markup, but should not be.

    The key differences between CDATA and comments are:

    • As Richard points out, CDATA is still part of the document, while a comment is not.
    • In CDATA you cannot include the string ]]> (CDEnd), while in a comment -- is invalid.
    • Parameter Entity references are not recognized inside of comments.

    This means given these four snippets of XML from one well-formed document:

    <!ENTITY MyParamEntity "Has been expanded">
    

    <!--
    Within this comment I can use ]]>
    and other reserved characters like <
    &, ', and ", but %MyParamEntity; will not be expanded
    (if I retrieve the text of this node it will contain
    %MyParamEntity; and not "Has been expanded")
    and I can't place two dashes next to each other.
    -->
    

    <![CDATA[
    Within this Character Data block I can
    use double dashes as much as I want (along with <, &, ', and ")
    *and* %MyParamEntity; will be expanded to the text
    "Has been expanded" ... however, I can't use
    the CEND sequence. If I need to use CEND I must escape one of the
    brackets or the greater-than sign using concatenated CDATA sections.
    ]]>
    

    <description>An example of escaped CENDs</description>
    <!-- This text contains a CEND ]]> -->
    <!-- In this first case we put the ]] at the end of the first CDATA block
         and the > in the second CDATA block -->
    <data><![CDATA[This text contains a CEND ]]]]><![CDATA[>]]></data>
    <!-- In this second case we put a ] at the end of the first CDATA block
         and the ]> in the second CDATA block -->
    <alternative><![CDATA[This text contains a CEND ]]]><![CDATA[]>]]></alternative>
    
    0 讨论(0)
  • 2020-11-21 08:06

    The data contained therein will not be parsed as XML, and as such does not need to be valid XML or can contain elements that may appear to be XML but are not.

    0 讨论(0)
  • 2020-11-21 08:09

    One big use-case: your xml includes a program, as data (e.g. a web-page tutorial for Java). In that situation your data includes a big chunk of characters that include '&' and '<' but those characters aren't meant to be xml.

    Compare:

    <example-code>
    while (x &lt; len &amp;&amp; !done) {
        print( &quot;Still working, &apos;zzz&apos;.&quot; );
        ++x;
        }
    </example-code>
    

    with

    <example-code><![CDATA[
    while (x < len && !done) {
        print( "Still working, 'zzzz'." );
        ++x;
        }
    ]]></example-code>
    

    Especially if you are copy/pasting this code from a file (or including it, in a pre-processor), it's nice to just have the characters you want in your xml file, w/o confusing them with XML tags/attributes. As @paary mentioned, other common uses include when you're embedding URLs that contain ampersands. Finally, even if the data only contains a few special characters but the data is very very long (the text of a chapter, say), it's nice to not have to be en/de-coding those few entities as you edit your xml file.

    (I suspect all the comparisons to comments are kinda misleading/unhelpful.)

    0 讨论(0)
提交回复
热议问题