ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: XML :: XML CDATA

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

XML
XML DHTMLÐÐΪ
XML Ïà¹Ø¼¼Êõ
XML ±à¼­Æ÷
XML ÕªÒª
XML ʵÀý
XML×Ö·û±àÂë
xml ÎĵµÊ÷
IEºÍ»ðºü¶ÁÈ¡XML·..

XML CDATA


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-29 ÐÞ¸Ä:2007-08-15 ä¯ÀÀ:4750 :: ::

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Ƭ¶ÏÄÚµÄÎı¾»á±»½âÎöÆ÷ºöÂÔ¡£


Parsed Data
±»½âÎöµÄÊý¾Ý

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>
<first>Bill</first>
<last>Gates</last>
</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 &lt; 1000 then</message>

There are 5 predefined entity references in XML:
ÔÚXMLÀ°üº¬5¸öÔ¤¶¨ÒåµÄʵÌå²ÎÊý£º

&lt; < less than[СÓÚ]
&gt; > greater than[´óÓÚ]
&amp; & ampersand [ºÍ]
&apos; ' apostrophe[Ê¡ÂÔºÅ]
&quot; " quotation mark[ÒýºÅ]


×¢Òâ:
Ö»ÓÐ×Ö·û"<" ºÍ "&" ÔÚXMLÀïÊÇÑϸñÒâÒåÉÏ·Ç·¨µÄ¡£Ê¡ÂÔ·û£¬ÒýºÅºÍ¸ü¸ß¼¶µÄ·ûºÅÊǺϷ¨µÄ£¬ÓÃ"<" ºÍ "&"´úÌæËüÃÇÊǸüΪ¼ò±ãµÄ·½·¨¡£


CDATA

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>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1
}
else
{
return 0
}
}
]]>

</script>

In the example above, everything inside the CDATA section is ignored by the parser.
ÔÚÉÏÊö°¸ÀýÖУ¬CDATAƬ¶ÏÖеÄËùÓÐÄÚÈݶ¼±»½âÎöÆ÷ºöÂÔÁË¡£

Notes on CDATA sections:
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.
ͬʱ»¹Òª×¢Ò⣺ÔÚ "]]>" ×Ö·û´®ÄÚ²»Ó¦¸Ã°üº¬¿Õ¸ñ»ò»»Ðмü¡£

ÆÀÂÛ (3) 1 All