7 Replies Latest reply on May 14, 2009 4:43 AM by Barna Biro

    Reading XML File

    Mohd Fayaz Level 1

      Hi,

       

      I have to read an XML file from server, that contains nodes to be added in a tree, and then add nodes to the tree. Can anyone please tell me how to do that.

       

      Thanks in advance

        • 1. Re: Reading XML File
          ATIF FAROOQ Level 3

          AOA

              this example will help you to acomplish that

           

           

          <?xml version="1.0" encoding="utf-8"?>
          <!-- Tree control example. -->
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
          
              <mx:Script>
                  <![CDATA[
          
                      [Bindable]
                      public var selectedNode:XML;
          
                      // Event handler for the Tree control change event.
                      public function treeChanged(event:Event):void {
                          selectedNode=Tree(event.target).selectedItem as XML;
                      }
                  ]]>
              </mx:Script>
          
              <mx:XMLList id="treeData">
                  <node label="Mail Box">
                      <node label="Inbox">
                          <node label="Marketing"/>
                          <node label="Product Management"/>
                          <node label="Personal"/>
                      </node>
                      <node label="Outbox">
                          <node label="Professional"/>
                          <node label="Personal"/>
                      </node>
                      <node label="Spam"/>
                      <node label="Sent"/>
                  </node>    
              </mx:XMLList>
          
              <mx:Panel title="Tree Control Example" height="75%" width="75%" 
                  paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">
          
                  <mx:Label width="100%" color="blue" 
                      text="Select a node in the Tree control."/>
          
                  <mx:HDividedBox width="100%" height="100%">
                      <mx:Tree id="myTree" width="50%" height="100%" labelField="@label"
                          showRoot="false" dataProvider="{treeData}" change="treeChanged(event)"/>
                      <mx:TextArea height="100%" width="50%"
                          text="Selected Item: {selectedNode.@label}"/>
                  </mx:HDividedBox>
                  
              </mx:Panel>
          </mx:Application>
          
          • 2. Re: Reading XML File
            Barna Biro Level 3

            Hi there,

             

            You can read XML files with the URLLoader class:

             

            http://livedocs.adobe.com/flex/3/langref/flash/net/URLLoader.html

            http://livedocs.adobe.com/flex/3/html/help.html?content=17_Networking_and_communications_3 .html

             

            You will most probably need a crossdomain.xml policy file that assures your app that the place you are trying to load the data from is trusted.

             

            http://labs.adobe.com/wiki/index.php/Flex:Accessing_Data

            http://www.cflex.net/showFileDetails.cfm?ObjectID=287&Object=Fi

             

            Once you get all that done. You can read the .xml file from the server. When it's received, you can parse it and store needed data in an ArrayCollection or something or you can leave it as an XML structure and work on it directly.

             

            With best regards,

            Barna Biro

            Blog: http://blog.wisebisoft.com

            1 person found this helpful
            • 3. Re: Reading XML File
              Richard_Abbott Level 3

              Hi Mohd, Atif, Barna and all,

               

              Barna wrote "You will most probably need a crossdomain.xml policy file that assures your app that the place you are trying to load the data from is trusted": an alternative is to make the call specifically to your own web server, and then get the server code-behind to make any remote calls that are needed. This covers you in situations where you may not have sufficient access to the original source in order to place the cross-domain trust file. Once your own server has pushed the data down to the client then the ways Atif and Barna have suggested will do the job very nicey.

              Richard

              • 4. Re: Reading XML File
                ATIF FAROOQ Level 3

                Hi,

                    This example will help you in acomplishing taht

                 

                             <?xml version="1.0" encoding="utf-8"?>

                <!-- Tree control example. -->

                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

                 

                    <mx:Script>

                        <![CDATA[

                 

                           

                            public var selectedNode:XML;

                 

                            // Event handler for the Tree control change event.

                            public function treeChanged(event:Event):void {

                                selectedNode=Tree(event.target).selectedItem as XML;

                            }

                        ]]>

                     

                            <mx:Tree id="myTree" width="50%" height="100%"

                labelField="@label"

                                showRoot="false" dataProvider=""

                change="treeChanged(event)"/>

                            <mx:TextArea height="100%" width="50%"

                                text="Selected Item: {selectedNode.@label}"/>

                        </mx:HDividedBox>

                 

                    </mx:Panel>

                </mx:Application

                • 5. Re: Reading XML File
                  Mohd Fayaz Level 1

                  Thanks to all you guys for your replies.

                   

                  @Barna Biro

                  Can you please further explain me your approach.

                  The URLLoader is returning the file as an object. Now, how can I convert that into an XML file/ XMList / XMLListCollection.

                  • 6. Re: Reading XML File
                    Barna Biro Level 3

                    You could simply add this into your load complete handler ( the one that is triggered once the loaded data is received ):

                     

                    var loadedData:XML = XML(event.target.data);
                    

                     

                    Event better, you'd define the loadedData variable somewhere outside the method so you can access it from different methods too. Once all this is done, you have your XML structure and you can do whatever you want with it ( access nodes, delete nodes and so on ).

                     

                    With best regards,

                    Barna Biro

                    Blog: http://blog.wisebisoft.com

                    • 7. Re: Reading XML File
                      Mohd Fayaz Level 1

                      Yes, this is working. Thanks a lot.