FlexµÄ±³¾°ºÍ¼¼ÊõÌصã(7)
1.7 ÔÚFlexÖвÙ×÷XML
XML(eXtensible Markup Language£¬¿ÉÀ©Õ¹±ê¼ÇÓïÑÔ)³£ÓÃÓÚÊý¾ÝÃèÊö¡¢Êý¾Ý·â×°ºÍÊý¾Ý½á¹¹»¯´¦Àí¡£XMLÌṩÁ˼òµ¥ºÍÒ׶ÁµÄÊý¾Ý×éÖ¯·½Ê½£¬¿ÉÒÔ¸ü¼Ó·½±ãºÍ±ê×¼µØ¶ÔÊý¾Ý½øÐжÁÈ¡¡¢´«ÊäºÍ²Ù×÷¡£Flex¼¼ÊõÖÐʹÓõÄMXMLÒ²ÊÇ»ùÓÚXMLµÄÒ»ÖÖ±êÇ©¹æ·¶¡£
1.7.1 XMLÔÚFlexÖеÄÓ¦ÓÃ
ÔÚFlexÖд¦ÀíXMLÊý¾ÝÐèÒªÀí½âһЩ»ù±¾µÄXML¸ÅÄî¡£ÔÚ±¾ÊéÖлáÉæ¼°µ½µÄXML¸ÅÄîÈçÏ¡£
1. ÔªËØ
ÔªËØÊÇXMLÊý¾ÝÖеÄÒ»¸öÊý¾ÝÌõÄ¿£¬Í¨¹ýÒ»¶Ô±êÇ©ºÍ±êÇ©ÄÚµÄ×ÓÔªËض¨Òå¡£XMLµÄÔªËØÖпÉÒÔ°üº¬Îı¾Êý¾Ý»òÆäËûµÄÔªËØ£¬Ò²¿ÉÒÔΪ¿Õ¡£
2. ¿ÕÔªËØ
¿ÕÔªËØÊDz»°üº¬×ÓÔªËصÄXMLÔªËØ¡£Í¨³£XMLµÄ¿ÕÔªËØ¿ÉÒÔд³É×Ô·â±ÕµÄ¸ñʽ£¬ÀýÈç<emptyitem/>¡£
3. Îĵµ
ÎĵµÊÇÒ»¸ö¶ÀÁ¢µÄXML½á¹¹¡£Ò»¸öXMLÎĵµ¿ÉÒÔ°üº¬ÈÎÒâÊýÁ¿µÄXMLÔªËØ£¬µ«ÊDZØÐëÖ»°üº¬Î¨Ò»µÄ¸ùÔªËØ¡£
4. ½Úµã
½ÚµãºÍÔªËصÄÒâÒåÏàͬ¡£
5. ÊôÐÔ
ÊôÐÔÊǶԱêÇ©ÄÚµÄÒ»¸öXMLÔªËØÌõÄ¿µÄÃüÃû¡£Í¨¹ý ÊôÐÔÃû=“Öµ”µÄ¸ñʽ½øÐж¨Òå¡£¿ÉÒÔ¿´×÷ÊÇλÓÚÔªËØÄÚ²¿µÄ×ÓÔªËØ¡£
FlexΪ´¦ÀíXMLÌṩÁ˱ØÒªµÄÖ§³Ö£¬ActionScriptÀàXMLºÍXMLListÓÃÓÚ½¨Á¢FlexµÄXML¶ÔÏó¡£XMLÀàÓÃÓÚÃèÊöµ¥Ò»µÄXML¶ÔÏó£¬ÈçXMLµÄÒ»¸ö½ÚµãÊý¾ÝÒÔ¼°Õâ¸ö½ÚµãµÄËùÓÐAttributeÖµºÍϼ¶½ÚµãÐÅÏ¢£»XMLListÀàÓÃÓÚÃèÊöÒ»×éXMLÔªËØ£¬Õâ×éÔªËØÐèÒªÓÐÏàͬµÄXML±êÇ©£¬Í¨¹ýXMLList¿ÉÒÔÁé»îµØ¶ÔXMLÔªËؼ¯ºÏ½øÐÐÌí¼Ó¡¢É¾³ý¡¢Ð޸ĺͲéѯ²Ù×÷¡£
ͨ¹ýMXMLÎÒÃÇ¿ÉÒÔʹÓÃ<mx:XML>±êÇ©ºÍ<mx:XMLList>±êÇ©¹¹Ô쾲̬µÄXML½á¹¹¡£ActionScriptÔò¿ÉÒÔ¶ÔMXMLµÄ¾²Ì¬XML¶ÔÏó»òÍⲿÎĵµ½øÐи÷ÖÖÊý¾Ý´¦Àí²Ù×÷£¬¶ÔÓڴ󲿷ÖXMLÊý¾ÝµÄ´¦Àí²Ù×÷¶¼ÊÇͨ¹ýActionScriptÍê³É¡£
ͨ³£ActionScript¿ÉÒÔÍê³ÉÏÂÁйØÓÚXMLµÄ²Ù×÷£º
- ¹¹½¨XMLÎĵµ£¬Ìí¼ÓXMLÔªËغÍÖµ¡£
- ·ÃÎÊXMLÔªËØ£¬ÊôÐÔ(Attribute)ºÍÖµ¡£
- ²éѯXMLÔªËØ¡£
- ±éÀúXMLÊý¾ÝµÄ¼¯ºÏ¡£
- ½øÐÐXMLÀàÐͺÍStringÀàÐ͵ÄÏ໥ת»»¡£
- ²Ù×÷XMLÃüÃû¿Õ¼ä¡£
- ¶ÁÈ¡ÍⲿµÄXMLÎļþ¡£
1.7.2 ͨ¹ýE4X´¦ÀíXML
ÎÒÃÇÒѾÁ˽⵽ActionScriptÊÇ»ùÓÚ±ê×¼µÄECMAScriptÓïÑԹ淶¹¹½¨µÄ£¬Òò´ËÒ²ÑØÏ®ÁËECMAScript´¦ÀíXMLµÄAPI×Ó¼¯ECMAScript for XML£¬¼ò³ÆE4X¡£E4X°üº¬ÏÂÁÐÀàÐÍÓÃÓÚ´¦ÀíXML£ºXML¡¢XMLList¡¢QNameºÍNamespace¡£
1. ´´½¨E4XµÄXML¶ÔÏó
E4X¶ÔÓÚXMLµÄÖ§³Ö×ñÑ׿ò½àÐÔ¡¢¸ß¾ÛºÏÐÔºÍÒ×ÓÃÐÔµÄÉè¼ÆÔÔò¡£Òò´ËʹÓÃE4X²¢Ã»ÓкܸߵÄÄѶȡ£Ê×ÏÈÎÒÃÇÀ´¿´¿´ÈçºÎÓÃE4X´´½¨XMLÀàÐÍ£º
var xmlData:XML =
<product>
<item id = "0001">
<name>Thinkpad</name>
<price>24000</price>
</item>
<item id = "0001">
<name>Thinkpad</name>
<price>24000</price>
</item>
</product>
ÉÏÊö´úÂë´´½¨ÁËÒ»¸ö±äÁ¿ÃûΪxmlDataµÄE4XµÄXML¶ÔÏó¡£¹¹½¨·½Ê½ÊǷdz£¼òµ¥µÄ£¬ºÍ¶¨ÒåÒ»¸ö¼òµ¥ÀàÐ͵ıäÁ¿Ã»ÓÐÈκÎÇø±ð£¬Ö±½Ó°ÑXMLµÄÄÚÈÝ°´Ë³ÐòдÔڵȺÅÓұ߼´¿É£¬E4X»áÔÚ±àÒëActionScriptʱ¶ÔXMLµÄºÏ·¨ÐÔ½øÐÐÑéÖ¤£¬²»ºÏ·¨µÄXML»áÔÚ±àÒë¹ý³ÌÖб¨´í¡£
2. ²Ù×÷E4XµÄXMLÀàÐÍ
E4XµÄXML¶ÔÏóÃèÊöÁËXMLµÄÊý¾Ý½á¹¹¡¢½Úµã¡¢Îı¾½Úµã¡¢ÊôÐÔ/ÊôÐÔÖµ¡¢²Ù×÷Ö¸Áî¡¢×¢ÊÍÐÅÏ¢µÈÄÚÈÝ¡£²»°üº¬²Ù×÷Ö¸ÁîºÍ×¢ÊÍÐÅÏ¢µÄXML¶ÔÏ󱻶¨ÒåΪ¼òµ¥XML¶ÔÏó£¬ÔÚ¹¹½¨XML¶ÔÏóʱ£¬¿ÉÒÔͨ¹ýÉ趨ÏÂÁÐÓï¾äÀ´ÊµÏÖ¼òµ¥¶ÔÏóµÄ´´½¨£º
XML.ignoreComments = true;
XML.ignoreProcessingInstructions = true;
°üº¬²Ù×÷Ö¸ÁîºÍ×¢ÊÍÐÅÏ¢µÄXML¶ÔÏ󱻶¨ÒåΪ¸´ÔÓXML¶ÔÏ󣬿ÉÒÔͨ¹ýcomments()·½·¨ºÍprocessingInstructions()·½·¨À´´¦Àí×¢ÊͺͲÙ×÷Ö¸ÁîÄÚÈÝ¡£
°üÀ¨ÉÏÊöÁ½¸öÊôÐÔ£¬E4XµÄXML¶ÔÏó¹²°üº¬5¸ö¾²Ì¬ÊôÐÔ£¬ÓÃÓÚ³õʼ»¯É趨»ò¶¨ÖƲÙ×÷XMLµÄ¾ßÌå¸ñʽ¡£ÊôÐÔÁоÙÈçÏ¡£
- ignoreComments£º
¸ÃÊôÐÔÓÃÓÚÖ¸¶¨ÊÇ·ñ´¦ÀíXML×¢ÊÍ¡£ - ignoreProcessingInstructions£º
¸ÃÊôÐÔÓÃÓÚÖ¸¶¨ÊÇ·ñ´¦ÀíXML²Ù×÷Ö¸Áî¡£ - ignoreWhitespace£º
¸ÃÊôÐÔÓÃÓÚÖ¸¶¨ÊÇ·ñ´¦ÀíXMLÖеĿոñ·û¡£ - prettyIndent£º
¸ÃÊôÐÔÓÃÓÚÖ¸¶¨¶ÔtoString()·½·¨ÊÇ·ñ½øÐÐÊʵ±Ëõ½ø¡£ - prettyPrinting£º
¸ÃÊôÐÔÓÃÓÚÖ¸¶¨¶ÔtoXMLString()·½·¨ÊÇ·ñ½øÐÐÊʵ±Ëõ½ø¡£
XML¶ÔÏó½á¹¹µÄ½âÎöºÍ´¦Àíͨ¹ýE4XµÄ·½·¨À´ÊµÏÖ¡£E4XΪXML¶ÔÏóÌṩÁ˷dz£ÍêÉƵķ½·¨Ö§³Ö¡£Í¨¹ýchildren()¡¢elements()¡¢parent()·½·¨¿ÉÒÔÒýÓõ½XML¶ÔÏóÖ®ÖеÄXMLList¶ÔÏó¡£±¾ÊéÖжÔE4XµÄ¾ßÌåAPI²»Ïêϸ½²½â£¬¶ÁÕßÈçÓÐÐèÒª¿É²Î¿¼ECMA357Óû§Êֲᡣ
3. ²Ù×÷E4XµÄXMLListÀàÐÍ
XMLListÀàÐͱíÃ÷ÕâÊÇÒ»¸ö¾ßÓÐÏàͬ½á¹¹µÄXMLÔªËؼ¯ºÏ¡£XMLList¶ÔÏóͬÑùÖ§³Öchildren()¡¢elements()¡¢parent()µÈÕë¶ÔÉÏϼ¶½ÚµãµÄË÷Òý¡£Í¬Ê±£¬XMLList¶ÔÏó»¹Ö§³Öattribute()¡¢attributes()·½·¨À´²Ù×÷×ÔÉíµÄÊôÐÔÐÅÏ¢¡£