w3pop.com :: ÍøÂçѧԺ :: XML :: XML CDATA
All text in an XML document will be parsed by the parser.
XMLÎĵµÖеÄËùÓÐÎı¾¶¼»á±»½âÎöÆ÷½âÎö¡£
Only text inside a CDATA section will be ignored by the parser.
Ö»ÓÐÔÚCDATAƬ¶ÏÄÚµÄÎı¾»á±»½âÎöÆ÷ºöÂÔ¡£
XML parsers normally parse all the text in an XML document.
XML½âÎöÆ÷ͨ³£»á½âÎöXMLÎĵµÄÚµÄËùÓÐÎı¾ÄÚÈÝ¡£
When an XML element is parsed, the text between the XML tags is also parsed:
µ±XMLÔªËØ±»½âÎöʱ£¬XML±êǩ֮¼äµÄÎı¾ÄÚÈÝÒ²»á±»½âÎö£º
<message>This text is also parsed</message> |
The parser does this because XML elements can contain other elements, as in this example, where the <name> element contains two other elements (first and last):
½âÎöÆ÷ÕâÑù×öÊÇÒòΪXMLÔªËØÖпÉÄܰüº¬ÆäËüµÄÔªËØ¡£ÈçÔÚÏÂÃæÕâ¸ö°¸ÀýÖУ¬<name>ÔªËØº¬ÓÐÆäËüÁ½¸öÔªËØ£¨µÚÒ»¸öºÍ×îºóÒ»¸ö£©£º
<name><first>Bill</first><last>Gates</last></name> |
and the parser will break it up into sub-elements like this:
½âÎöÆ÷»á°ÑËü·Ö½â³ÉÏÂÃæÕâÑùµÄ×ÓÔªËØ£º
<name> |
Escape Characters
תÒå·û
Illegal XML characters have to be replaced by entity references.
·Ç·¨µÄXML×Ö·û±ØÐëÓÉʵÌå²ÎÊýÌæ´ú¡£
If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element. You cannot write something like this:
Èç¹ûÄãÔÚÒ»¸öXMLÔªËØÖзÅÖÃÒ»¸ö "<" ×Ö·û£¬¾Í»á²úÉú´íÎó£¬ÒòΪ½âÎöÆ÷»áÎóÈÏΪËüÊÇÐÂÔªËØµÄ¿ªÊ¼¡£Äã²»¿ÉÒÔд³ÉÏÂÃæÕâÑù£º
<message>if salary < 1000 then</message> |
To avoid this, you have to replace the "<" character with an entity reference, like this:
ΪÁ˱ÜÃâÕâÖÖÇé¿öµÄ·¢Éú£¬Äã±ØÐëʹÓÃÒ»¸öʵÌå²ÎÊýÀ´Ìæ»» "<" £¬ÈçÏÂËùʾ£º
<message>if salary < 1000 then</message> |
There are 5 predefined entity references in XML:
ÔÚXMLÀ°üº¬5¸öÔ¤¶¨ÒåµÄʵÌå²ÎÊý£º
| < | < | less than[СÓÚ] |
| > | > | greater than[´óÓÚ] |
| & | & | ampersand [ºÍ] |
| ' | ' | apostrophe[Ê¡ÂÔºÅ] |
| " | " | quotation mark[ÒýºÅ] |
×¢Òâ: Ö»ÓÐ×Ö·û"<" ºÍ "&" ÔÚXMLÀïÊÇÑϸñÒâÒåÉÏ·Ç·¨µÄ¡£Ê¡ÂÔ·û£¬ÒýºÅºÍ¸ü¸ß¼¶µÄ·ûºÅÊǺϷ¨µÄ£¬ÓÃ"<" ºÍ "&"´úÌæËüÃÇÊǸüΪ¼ò±ãµÄ·½·¨¡£
Everything inside a CDATA section is ignored by the parser.
ÔÚCDATAƬ¶ÏÄÚµÄÒ»ÇÐÄÚÈݶ¼»á±»½âÎöÆ÷ºöÂÔ¡£
If your text contains a lot of "<" or "&" characters - as program code often does - the XML element can be defined as a CDATA section.
Èç¹ûÄãµÄÎı¾Öаüº¬´óÁ¿µÄ "<" »ò "&" ×Ö·û——¾ÍÏó³ÌÐòÂëÖо³£µÄÄÇÑù——XMLÔªËØ¿ÉÒÔ±»¶¨ÒåΪһ¸öCDATA²¿·Ö¡£
A CDATA section starts with "<![CDATA[" and ends with "]]>":
Ò»¸öCDATAƬ¶ÏÓÉ "<![CDATA[" ¿ªÊ¼£¬ÓÉ "]]>" ½áÊø£º
<script> |
In the example above, everything inside the CDATA section is ignored by the parser.
ÔÚÉÏÊö°¸ÀýÖУ¬CDATAƬ¶ÏÖеÄËùÓÐÄÚÈݶ¼±»½âÎöÆ÷ºöÂÔÁË¡£
A CDATA section cannot contain the string "]]>", therefore, nested CDATA sections are not allowed.
CDATAƬ¶Ï²»Äܰüº¬×Ö·û´® "]]>"£¬Òò´Ë£¬Ç¶Ì×CDATAƬ¶ÏÊDz»ÔÊÐíµÄ¡£
Also make sure there are no spaces or line breaks inside the "]]>" string.
ͬʱ»¹Òª×¢Ò⣺ÔÚ "]]>" ×Ö·û´®ÄÚ²»Ó¦¸Ã°üº¬¿Õ¸ñ»ò»»Ðмü¡£