When manipulating an XML file, allowing the user to enter content such as "<", ">", "/", "", and so on, when the XML is generated, destroys the XML structure and interrupts the data.
All text in an XML document is parsed by the parser, which is used in XML CDATA, and only the text within the CDATA part is ignored by the parser.
1. Parsing text
The XML parser typically processes all the text in an XML document.
When the XML element is parsed, the text inside the XML element is also parsed:<message>this text is also parsed</message>.
The XML parser does this because the XML element may also contain other elements, as in the following example, the name element contains the first and last two elements inside:
<name>
<first>Bill</first>
<last>Gates</last>
</name>
The parser will assume that the above code is this:
<name>
<first>Bill</first>
<last>Gates</last>
</name>
2. Escape character
An illegal XML character must be replaced with the corresponding entity.
If you use a character like "<" in an XML document, the parser will get an error because the parser will assume that this is the beginning of a new element. Therefore, you should not write the code as follows:
<message>if Salary < Then</message>
To avoid this situation, the character "<" must be converted to an entity, as follows:
< Message>if salary < then</message>
Here are five pre-defined entities in the XML document:
< < less sign
> > Greater than Sign
& & and
' ' Single quotation mark
"" Double quotation marks
The entity must begin with the symbol "&", with the symbol ";" End.
Note: Only the "<" character and the "&" character are strictly forbidden for XML. The rest is legal, and the use of entities is a good habit in order to reduce errors.
3.CDATA Parts
All content inside CDATA is ignored by the parser.
If the text contains a lot of "<" characters and "&" characters-just like the program code-it's best to put them all in a CDATA widget.
A CDATA widget with "<! The [cdata[] tag begins with the "]]>" Mark:
<script>
<! [cdata[
function Matchwo (A, b) {
if (A < b && a < 0) then {
Return 1
} else {
return 0
}
}
]]>
</script>
In the previous example, all the text between the CDATA parts is ignored by the parser.
CDATA Considerations:
CDATA Parts can no longer contain CDATA parts (not nested). If the CDATA part contains the character "]]>" or "<! [cdata[], will be very likely to make mistakes oh.
Also note that there is no space or line break between the string "]]>".
The role of CDATA in XML files