Mar 05

下面的例子展示了如何利用HTTPService MXML标签(tag),通过简单的设置resultFormat属性为“object”,在读取XML文件的同时,将其转化为ActionScript对象。

下面是全部的代码:


Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- http://blog.flexexamples.com/2007/09/19/converting-xml-to-objects-using-the-flex-httpservice-mxml-tag/ -->
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  4.         layout="vertical"
  5.         verticalAlign="middle"
  6.         backgroundColor="white"
  7.         creationComplete="serv.send();">
  8.  
  9.     <mx:Script>
  10.         <![CDATA[
  11.             import mx.rpc.events.FaultEvent;
  12.             import mx.rpc.events.ResultEvent;
  13.  
  14.             private function serv_result(evt:ResultEvent):void {
  15.                 var resultObj:Object = evt.result;
  16.                 /* Assign the values... */
  17.                 nameText.text = resultObj.album.name;
  18.                 img0Text.text = resultObj.album.images.image[0];
  19.                 img1Text.text = resultObj.album.images.image[1];
  20.                 img2Text.text = resultObj.album.images.image[2];
  21.             }
  22.  
  23.             private function serv_fault(evt:FaultEvent):void {
  24.                 /* Show the error label. */
  25.                 error.text += evt.fault.faultString;
  26.                 error.visible = true;
  27.                 /* Hide the form. */
  28.                 form.visible = false;
  29.             }
  30.         ]]>
  31.     </mx:Script>
  32.  
  33.     <mx:String id="XML_URL">album.xml</mx:String>
  34.  
  35.     <mx:HTTPService id="serv"
  36.             url="{XML_URL}"
  37.             resultFormat="object"
  38.             result="serv_result(event);"
  39.             fault="serv_fault(event);" />
  40.  
  41.     <mx:ApplicationControlBar dock="true">
  42.         <mx:Label text="{XML_URL}" />
  43.     </mx:ApplicationControlBar>
  44.  
  45.     <mx:Label id="error"
  46.             color="red"
  47.             fontSize="36"
  48.             fontWeight="bold"
  49.             visible="false"
  50.             includeInLayout="{error.visible}"/>
  51.  
  52.     <mx:Form id="form"
  53.             includeInLayout="{form.visible}">
  54.         <mx:FormItem label="resultObj.album.name:">
  55.             <mx:Label id="nameText" />
  56.         </mx:FormItem>
  57.         <mx:FormItem label="resultObj.album.images.image[0]:">
  58.             <mx:Label id="img0Text" />
  59.         </mx:FormItem>
  60.         <mx:FormItem label="resultObj.album.images.image[1]:">
  61.             <mx:Label id="img1Text" />
  62.         </mx:FormItem>
  63.         <mx:FormItem label="resultObj.album.images.image[2]:">
  64.             <mx:Label id="img2Text" />
  65.         </mx:FormItem>
  66.     </mx:Form>
  67.  
  68. </mx:Application>

这是例子中使用的XML文件

Download: album.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <album>
  3.     <name>One</name>
  4.     <images>
  5.         <image>image1.jpg</image>
  6.         <image>image2.jpg</image>
  7.         <image>image3.jpg</image>
  8.     </images>
  9. </album>

下面是执行效果:

原文作者:Peter deHaan 翻译:minidxer

written by Minidxer  |  tags: , , , , , ,

Related Post

One Response to “Flex中利用HTTPService MXML标签(tag)将XML转化为对象(objects)”

Trackbacks

Leave a Reply