Skip navigation
edua91
Currently Being Moderated

Loading local XML file in flash builder

Oct 6, 2009 10:12 PM

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?

 
Replies
  • Currently Being Moderated
    Oct 6, 2009 11:26 PM   in reply to edua91

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

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 7, 2009 6:16 AM   in reply to edua91

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 7, 2009 10:01 AM   in reply to edua91

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 7, 2009 10:51 AM   in reply to edua91

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 9, 2009 8:10 AM   in reply to mewk

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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (2)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points