What is XML <! [CDATA []]> for?

Asked

Viewed 26,770 times

17

In some examples of platforms, has among the tags the <![CDATA []]>, but what’s it for in XML after all?

Template for an XML to be followed by a platform.

<?xml version="1.0" encoding="UTF-8"?>
<loja>
   <produto>
      <link><![CDATA[http://www.loja.com.br/blusas/blusa-feminina?idparceiro=P1]]></link>
      <imagem><![CDATA[http://www.loja.com.br/imagens/blusa-feminina-1.jpg]]></imagem>
      <nome><![CDATA[Blusa feminina vermelha]]></nome>
      <categoria><![CDATA[Blusas Femininas]]></categoria>
      <descricao><![CDATA[Blusa feminina com gola em V e detalhes em strass.]]></descricao>
      <valor><![CDATA[119,00]]></valor>
      <valor_promo><![CDATA[95,90]]></valor_promo>
      <parcelamento><![CDATA[2x R$47,95]]></parcelamento>
      <cor><![CDATA[Vermelho, Azul, Branco]]></cor>
      <marca><![CDATA[Tommy Hilfiger]]></marca>
      <modelo><![CDATA[Único]]></modelo>
      <material><![CDATA[100% algodão]]></material>
      <tamanho><![CDATA[P, M, G]]></tamanho>
      <sexo><![CDATA[Feminino]]></sexo>
   </produto>
</loja>

1 answer

25


The CDATA serves to indicate that the text within its area is a common text and cannot be interpreted as part of the XML markup.

This is useful when a part of the text of a particular element can be confused with parts of the XML markup, which is not the case for the example shown. It’s a way to escape the characters.

There must be a specific reason for the platform to use indiscriminately. Perhaps because of the failure of their specification. Perhaps because they have no control over the content and there may be conflicting characters (it may contain HTML). Perhaps for some reason only they can explain (probably via documentation). What is certain is that it should be followed if they send.

An example of good use would be:

<secao><![CDATA[<sexo>Feminino</sexo>]]><secao>

What appears to be a tag XML is just a common text that happens to look like, and probably represents another XML.

Wikipedia.

  • I understand that since the CDATA snippet can appear anywhere in the XML where it can go text, such as between two tags, I believe the general guideline is to search in the XML Parsing you are using if there is the feature ready to escape this CDATA content in the text brought to a particular component of the XML tree..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.