<![CDATA[]]> and escape characters in xml documents

 

The content contained by <![CDATA[]]>this tag will be represented as plain text , i.e. <![CDATA[<]]>text content “<”
  This tag is used in xml documents, let's take a look at the use of escape characters. We know that in xml, characters such as ”<”, ”>”, ”&”etc. cannot be stored directly, otherwise an error will be reported during the xml syntax check . If you want to use these symbols in xml, you must escape them as entities, such as ”&lt;”, ”&gt;”, ”&amp;”, so that you can save them into the xml document. 
  When read by the program, the parser will automatically convert these entities back to ”<”, ”>”, ”&”. For example:    the above writing method will report an error, it should be written like this   It is worth noting that:    (1) There can be no spaces between escape sequence characters;    (2) The escape sequence must end with ";";    (3) Separate The occurrence of "&" is not considered to be the beginning of an escape;    (4) case-sensitive.   In XML, characters that need to be escaped are:    (1)&     (2)<     (3)>     (4)"     (5)'     But strictly speaking, only "<" and "&" are illegal in XML , the other three can exist legally, but it is a good practice to escape them all. 
  <age> age < 30 </age> 

  <age> age &lt; 30 </age> 





   

&amp; 
&lt; 
&gt; 
&quot; 
&apos; 

  In any case, the characters before escaping and the characters after escaping will be parsed by the xml parser. For convenience , it is used <![CDATA[]]>to include content that is not parsed by the xml parser. But it should be noted that: 
  (1) This part cannot contain any more ”]]>”
  (2) Nesting is not allowed; 
  (3) ”]]>”This part cannot contain spaces or newlines. 
  Finally, let's talk about <![CDATA[]]>the relationship with xml transfer characters. Do the two of them seem to have duplicate functions? 
  Yes, their functions are the same, but the application scenarios and requirements are slightly different: 
  (1) It is <![CDATA[]]>not applicable to all situations, but escape characters are OK; 
  (2) <![CDATA[]]>It is verbose for short strings, and for long strings, escape characters are written. It is less readable; 
  (3)  <![CDATA[]]>means that the xml parser ignores parsing, so it is faster.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326336494&siteId=291194637