Skip navigation
Home/Support/

Forums

4996 Views 8 Replies Latest reply: Oct 10, 2009 6:11 PM by edua91 RSS
edua91 User 16 posts since
Jun 10, 2009
Currently Being Moderated

Oct 6, 2009 11:12 PM

Loading local XML file in flash builder

Hi, iam almost new to flex and flash builder, i am creating an app that loads a local xml file, but i dont know how can i do this on all the new flash builder interface, what url i have to put on the data services panel in order to acces the file and create the xml data type?

  • Harpreet (Adobe) Adobe Employee 24 posts since
    Jun 2, 2009
    Currently Being Moderated
    1. Oct 7, 2009 12:26 AM (in response to edua91)
    Re: Loading local XML file in flash builder

    Put the local file in the bin-debug folder of the project. Then in the Service Wizard specify the filename as url.

  • mewk User 164 posts since
    Jul 20, 2009
    Currently Being Moderated
    2. Oct 7, 2009 7:16 AM (in response to edua91)
    Re: Loading local XML file in flash builder

    Hey edua91,

     

    There are many many ways to load an xml file into your app. Let's assume you have the following .xml file in data/my_file.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <people>
       <person>
           <name>James</name>
           <age>24</age>
       </person>
       <person>
           <name>Cynthia</name>
           <age>33</age>
       </person>
    </people>
    

     

    Here are some methods:

         1. (simple mxml)

    1. <fx:Declarations>
          <fx:XML id="myXml" source="data/my_file.xml" />
      </fx:Declarations>
      

         2. (use HTTPService, as seen here)

    1. <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           initialize="myHttpService.send()">
      
           <fx:Script>
                <![CDATA[
                      import mx.collections.ArrayCollection;
                      import mx.rpc.events.ResultEvent;
      
                      public var myData:ArrayCollection;
      
                      protected function myHttpService_resultHandler(event:ResultEvent):void {
                           myData = event.result.people.person;
                      }
                 ]]>
           </fx:Script>
               <fx:Declarations>
                    <s:HTTPService id="myHttpService"
                                   url="data/my_file.xml"
                                   result="myHttpService_resultHandler(event)"/>
               </fx:Declarations>
      </s:Application>
      

         3. (embed in actionscript)

    1. <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           initialize="init()">
      
           <fx:Script>
               <![CDATA[
                     
                    [Embed(source=data/my_file.xml, mimeType=application/octet-stream)]
                    public var MyXMLData:Class;
                    public var myData:XML;
                     
                    public function init():void {
                       var byteArray:ByteArray = new MyXMLData() as ByteArray;
                       myData = new XML(byteArray.readUTFBytes(byteArray.length));
                    }
      
               ]]>
           </fx:Script>
      
      </s:Application>
      

         4. (using data/services panel!!!! what i really wanted to tell you)

      • start with a new application and add my_file.xml as done previously
      • Data/Services >>>>>> connect to data/service
      • HTTP
      • Operation: getData     ||||||||   URL: data/my_file.xml
      • Service name: myLocalDataService
      • finish data/services
      • goto design view. add a dataGrid to the app
      • in properties panel click the 'bind to data'
      • select the service we created and configure the return type
      • auto-detect >>>>> sample xml (window should display our my_file.xml contents)
      • select root should be person (not people).
      • finished!
      • if you want to bind the xml to a variable, you'll need to listen for a result event on the CallResponder

     

    1. <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                        xmlns:s="library://ns.adobe.com/flex/spark" 
                        xmlns:mx="library://ns.adobe.com/flex/halo" 
                        xmlns:mylocaldataservice="services.mylocaldataservice.*">
           
           <fx:Script>
                <![CDATA[
                     import mx.collections.ArrayCollection;
                     import mx.controls.Alert;
                     import mx.events.FlexEvent;
                     import mx.rpc.events.ResultEvent;
                     
                     public var myData:ArrayCollection;
      
                     protected function dataGrid_creationCompleteHandler(event:FlexEvent):void {
                          getDataResult.token = myLocalDataService.getData();
                     }
      
                     protected function getDataResult_resultHandler(event:ResultEvent):void {
                          myData = event.result as ArrayCollection;
                     }
      
                ]]>
           </fx:Script>
           <fx:Declarations>
                <s:CallResponder id="getDataResult" result="getDataResult_resultHandler(event)"/>
                <mylocaldataservice:MyLocalDataService id="myLocalDataService" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/>
           </fx:Declarations>
           <mx:DataGrid x="260" y="175" id="dataGrid" creationComplete="dataGrid_creationCompleteHandler(event)" dataProvider="{getDataResult.lastResult}">
                <mx:columns>
                     <mx:DataGridColumn headerText="name" dataField="name"/>
                     <mx:DataGridColumn headerText="age" dataField="age"/>
                </mx:columns>
           </mx:DataGrid>
           
      </s:Application>
      

     

    pretty cool, non?

     

    - e

  • David_F57 Contributor 1,540 posts since
    Apr 2, 2007
    Currently Being Moderated
    4. Oct 7, 2009 11:01 AM (in response to edua91)
    Re: Loading local XML file in flash builder

    it may help if you zip up your code and your xml file and attach it as it is difficult for anyone to determine what the problem is without seeing what it is you have done.

     

    David

  • mewk User 164 posts since
    Jul 20, 2009
    Currently Being Moderated
    6. Oct 7, 2009 11:51 AM (in response to edua91)
    Re: Loading local XML file in flash builder

    where is this file?! ;-p

     

    the wizard is different from beta1 to beta2

     

    in beta1 you need to enter a sample of the XML/JSON response. copy the contents of the my_file.xml into the text area box. that should get you on your way.

     

    - e

  • Anshulg User 22 posts since
    Oct 9, 2009
    Currently Being Moderated
    7. Oct 9, 2009 9:10 AM (in response to mewk)
    Re: Loading local XML file in flash builder

    Currently HTTP service wizard is the only option to import xml files. Future releases will have more intutive wizard to import XML files specifically.

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points