【转】Flex中利用SimpleXMLDecoder类将XML转化为对象(objects)

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-simplexmldecoder-class/ -->
  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.             import mx.rpc.xml.SimpleXMLDecoder;
  14.  
  15.             private function serv_result(evt:ResultEvent):void { 
  16.                 /* Convert XMLNode to XMLDocument. */ 
  17.                 var xmlStr:String = evt.result.toString();
  18.                 var xmlDoc:XMLDocument = new XMLDocument(xmlStr);
  19.                 var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
  20.                 var resultObj:Object = decoder.decodeXML(xmlDoc);
  21.                 /* Assign the values... */ 
  22.                 nameText.text = resultObj.album.name;
  23.                 img0Text.text = resultObj.album.images.image[0];
  24.                 img1Text.text = resultObj.album.images.image[1];
  25.                 img2Text.text = resultObj.album.images.image[2];
  26.             } 
  27.  
  28.             private function serv_fault(evt:FaultEvent):void { 
  29.                 // Show the error label.
  30.                 error.text += evt.fault.faultString;
  31.                 error.visible = true;
  32.                 // Hide the form.
  33.                 form.visible = false;
  34.             } 
  35.         ]]>
  36.     </mx:Script>
  37.  
  38.     <mx:String id="XML_URL">album.xml</mx:String>
  39.  
  40.     <mx:HTTPService id="serv" 
  41.             url="{XML_URL}" 
  42.             resultFormat="xml" 
  43.             result="serv_result(event);" 
  44.             fault="serv_fault(event);" />
  45.  
  46.     <mx:ApplicationControlBar dock="true">
  47.         <mx:Label text="{XML_URL}" />
  48.     </mx:ApplicationControlBar>
  49.  
  50.     <mx:Label id="error"
  51.             color="red"
  52.             fontSize="36"
  53.             fontWeight="bold"
  54.             visible="false"
  55.             includeInLayout="{error.visible}"/>
  56.  
  57.     <mx:Form id="form" 
  58.             includeInLayout="{form.visible}">
  59.         <mx:FormItem label="resultObj.album.name:">
  60.             <mx:Label id="nameText" />
  61.         </mx:FormItem>
  62.         <mx:FormItem label="resultObj.album.images.image[0]:">
  63.             <mx:Label id="img0Text" />
  64.         </mx:FormItem>
  65.         <mx:FormItem label="resultObj.album.images.image[1]:">
  66.             <mx:Label id="img1Text" />
  67.         </mx:FormItem>
  68.         <mx:FormItem label="resultObj.album.images.image[2]:">
  69.             <mx:Label id="img2Text" />
  70.         </mx:FormItem>
  71.     </mx:Form>
  72.  
  73. </mx:Application>

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>
原文地址:https://www.cnblogs.com/CoderWayne/p/1777907.html