7 Replies Latest reply on Apr 5, 2007 8:38 AM by Cancerinform

    Datagrid and external XML

    Cancerinform Level 2

      While it is simple to load data from a given xml file (myFile="<test>.......</test>";) to load into any components I have problems doing the same thing with XML loaded externally. The problem is probably that when the complete event in an Actionscript file has occurred the rest of the script was already executed.

      Does anybody have a simple example how to do that? Thanks.
        • 1. Re: Datagrid and external XML
          javamonjoe Level 1
          Not quite sure what the "rest of the script" or which complete event to which you are referring. You need an event to let you know when your xml file is loaded:

          xml_via_web.addEventListener(ResultEvent.RESULT, xmlFileLoadedMethod);

          I'm guessing that the rest of the script you mentioned is the processing of this xml data. Trigger the data from this xmlFileLoadedMethod instead of a creationComplete on your mxml class.

          Did this answer your question?

          • 2. Re: Datagrid and external XML
            Cancerinform Level 2
            Thanks for your answer. You are on the right track. What i meant is using for example this:

            var URL:String = "slideloader.xml";
            var urlRequest:URLRequest = new URLRequest(URL);
            urlLoader = new URLLoader();
            urlLoader.addEventListener("complete", onLoaded)
            function onLoaded(event:Event):void
            data = XML(urlLoader.data);

            I would then like to load children from data into a component like the datagrid. However, that does not work and my guess is because of time differences in the events.
            • 3. Re: Datagrid and external XML
              javamonjoe Level 1
              Ok. So you're doing an external file instead of an http request. It should be the same flow. Try something like this:
              var xmlData : XML = urlLoader.data as XML;
              trace( xmlData );

              in your onLoaded method and run debug. What happens? It's kind of tough to tell what is happening from here with the explanation of "it doesn't work."

              • 4. Re: Datagrid and external XML
                Cancerinform Level 2
                The data are correctly loaded and i can trace the XML data. May be I should change the way I load the XML. I just found this:


                I wonder that will solve my problem, although I like to know how to do it in Flex the way I originally started. I Flash 9 preview i have no problems, because it is pure AS :), which I know how to handle.

                I tried originally to convert the example from the datagrid class from the lang ref ( http://livedocs.adobe.com/flex/2/langref/). That example uses an internal XML, which is of course present when the datagrid will be loaded.
                • 5. Re: Datagrid and external XML
                  ntsiii Level 3
                  Yes, you need to be more specific than "it doesn't work". In your code, you are not assigning the data to the dataprovider of the DataGrid in the result handler. Are you doing that in your real code?
                  • 6. Re: Datagrid and external XML
                    Cancerinform Level 2

                    here is the example I started with from the language reference. Instead of having an XMLList object I wanted to load an XML file. I tried doing that with the above code. The dataProvider would then be the XML object. How do i get that to work?

                    <?xml version="1.0"?>
                    <!-- DataGrid control example. -->
                    <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">

                    <mx:XMLList id="employees">
                    <name>Christina Coenraets</name>
                    <name>Joanne Wall</name>
                    <name>Maurice Smith</name>
                    <name>Mary Jones</name>

                    <mx:Panel title="DataGrid Control Example" height="100%" width="100%"
                    paddingTop="10" paddingLeft="10" paddingRight="10">

                    <mx:Label width="100%" color="blue"
                    text="Select a row in the DataGrid control."/>

                    <mx:DataGrid id="dg" width="100%" height="100%" rowCount="5" dataProvider="{employees}">
                    <mx:DataGridColumn dataField="name" headerText="Name"/>
                    <mx:DataGridColumn dataField="phone" headerText="Phone"/>
                    <mx:DataGridColumn dataField="email" headerText="Email"/>

                    <mx:Form width="100%" height="100%">
                    <mx:FormItem label="Name">
                    <mx:Label text="{dg.selectedItem.name}"/>
                    <mx:FormItem label="Email">
                    <mx:Label text="{dg.selectedItem.email}"/>
                    <mx:FormItem label="Phone">
                    <mx:Label text="{dg.selectedItem.phone}"/>

                    • 7. Re: Datagrid and external XML
                      Cancerinform Level 2

                      I got it to work. I am in the learning stage. While i know AS3 pretty well I don't know yet how Flex exactly works. Using the above example this is the script, which has to be inserted. One important part is that the XML object has to be bound, didn't know that.

                      <?xml version="1.0"?>
                      <!-- DataGrid control example. -->
                      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" initialize="loadtheXML();">
                      import flash.events.*
                      import flash.net.*


                      public var myXML:XML;

                      private function loadtheXML():void
                      var URL:String = "employee.xml";

                      var request:URLRequest = new URLRequest(URL);
                      var xmlLoader:URLLoader = new URLLoader();
                      xmlLoader.addEventListener(Event.COMPLETE, onLoad);
                      function onLoad(e:Event):void
                      myXML = XML(e.target.data);

                      And for the dataprovider one needs to insert this: dataProvider="{myXML.employee}">

                      The original XML from that example would not work, because it is not valid XML lacking a root node. So one has to add a root node and employee would be the child nodes.

                      Thanks for all your help, it kept me working on it