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

前端 未结 13 2192
独厮守ぢ
独厮守ぢ 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 08:10

    Usually used for embedding custom data, like pictures or sound data within an XML document.

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

    The Cdata is a data which you may want to pass to an xml parser and still not interpreted as an 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 )

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

    Note that the CDATA construct is only needed if placing text directly in the XML text file.

    That is, you only need to use CDATA if hand typing or programmatically building the XML text directly.

    Any text entered using a DOM processor API or SimpleXML will be automatically escaped to prevent running foul of XML content rules.

    Notwithstanding that, there can be times where using CDATA can reduce the text size that would otherwise be produced with all entities encoded, such as for css in style tags or javascript in script tags, where many language constructs use characters in HTML|XML, like < and >.

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

    A CDATA section is "a section of element content that is marked for the parser to interpret as only character data, not markup."

    Syntactically, it behaves similarly to a comment:

    <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>
    

    ... but it is still part of the document:

    <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>
    

    Try saving the following as a .xhtml file (not .html) and open it using FireFox (not Internet Explorer) to see the difference between the comment and the CDATA section; the comment won't appear when you look at the document in a browser, while the CDATA section will:

    <?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>
    

    Something to take note of with CDATA sections is that they have no encoding, so there's no way to include the string ]]> in them. Any character data which contains ]]> will have to - as far as I know - be a text node instead. Likewise, from a DOM manipulation perspective you can't create a CDATA section which includes ]]>:

    var myEl = xmlDoc.getElementById("cdata-wrapper");
    myEl.appendChild(xmlDoc.createCDATASection("This section cannot contain ]]>"));
    

    This DOM manipulation code will either throw an exception (in Firefox) or result in a poorly structured XML document: http://jsfiddle.net/9NNHA/

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

    As another example of its use:

    If you have an RSS Feed (xml document) and want to include some basic HTML encoding in the display of the description, you can use CData to encode it:

    <item>
      <title>Title of Feed Item</title>
      <link>/mylink/article1</link>
      <description>
        <![CDATA[
          <p>
          <a href="/mylink/article1"><img style="float: left; margin-right: 5px;" height="80" src="/mylink/image" alt=""/></a>
          Author Names
          <br/><em>Date</em>
          <br/>Paragraph of text describing the article to be displayed</p>
        ]]>
      </description>
    </item>
    

    The RSS Reader pulls in the description and renders the HTML within the CDATA.

    Note - not all HTML tags work - I think it depends on the RSS reader you are using.


    And as a explanation for why this example uses CData (and not the appropriate pubData and dc:creator tags): this is for website display using a RSS widget for which we have no real formatting control.

    This enables us to specify the height and position of the included image, format the author names and date correctly, and so forth, without the need for a new widget. It also means I can script this and not have to add them by hand.

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

    From Wikipedia:

    [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.

    http://en.wikipedia.org/wiki/CDATA

    Thus: text inside CDATA is seen by the parser but only as characters not as XML nodes.

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