8 Replies Latest reply: Oct 10, 2009 5:11 PM by edua91 RSS

    Loading local XML file in flash builder

    edua91

      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?

        • 1. Re: Loading local XML file in flash builder
          hneelu Adobe Employee

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

          • 2. Re: Loading local XML file in flash builder
            mewk Community Member

            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

            • 3. Re: Loading local XML file in flash builder
              edua91 Community Member

              thank you guys but i still have a problem =/ when i want to configure the return type it gives me an error "The url is not valid", what should i do?

              i already did what you tell me on the method 4, but when i bind the data it ask me the return type, so on the option i select create a new and name it "person" click next, on that screen i dont know what to do so i click next again and there is where it gives me the error... =/

              • 4. Re: Loading local XML file in flash builder
                David_F57 CommunityMVP

                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

                • 5. Re: Loading local XML file in flash builder
                  edua91 Community Member

                  sry XD here is the file, btw iam using the beta 1

                  • 6. Re: Loading local XML file in flash builder
                    mewk Community Member

                    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

                    • 7. Re: Loading local XML file in flash builder
                      Anshulg

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

                      • 8. Re: Loading local XML file in flash builder
                        edua91 Community Member

                        thank you guys, i already installed beta 2 and had no problems ^-^