1 Reply Latest reply on May 31, 2009 10:49 PM by tsuyoshi.suzuki

    DataGrid with XML child nodes in AS3

    cheverSRQ

      I'm trying to populate a datagrid via an external xml file.  i'v gotten a good ways into it but have hit a stumbling block that i can't get past when it comes to using the child nodes to populate the datagrid.  thanks to wiki for the initial code and TreeHugger for the XMLList option.

       

      here is the as3 code...

      // Import the necessary packages
      import fl.controls.DataGrid;
      import fl.data.DataProvider;

      // --------------------- DataGrid init ----------------------------

      // Create a a new instance of DataGrid and name it "data_grid"
      var data_grid:DataGrid = new DataGrid();

      data_grid.addColumn("Date");
      data_grid.addColumn("Credit");
      data_grid.addColumn("Debit");
      var col3 = data_grid.addColumn("Master");
      var col4 = data_grid.addColumn("Desc");
      col3.minWidth = 150
      col4.minWidth = 200;
      // Fix the size
      data_grid.width = 650;
      // Set the height to five rows
      data_grid.rowCount = 5;
      // Position it on the stage
      data_grid.move(30, 40);
      // Then add it to the stage
      addChild(data_grid);

      // ------ DataProvider and XML loading code

      var dp:DataProvider;

      // define a URL and make it a request instance
      var url:String = "sortTest3.xml";
      var request:URLRequest = new URLRequest(url);

      // define a loader and have it load the request
      var url_loader:URLLoader = new URLLoader();
      url_loader.addEventListener(Event.COMPLETE, completeHandler);
      url_loader.load(request);

      // define a function that will execute after data has finished loading
      function completeHandler(event:Event):void {
          var ldr:URLLoader = event.currentTarget as URLLoader;
      // create XML datastructure from loaded XML
          var xmlDP:XML = new XML(ldr.data);

        

          var testList:XMLList = xmlDP.APR.children();
          var testString:String = testList.toXMLString();
          testString = "<April>"+testString+"</April>";
          var NewXML:XML = new XML(testString);

          dp = new DataProvider(NewXML);
          data_grid.dataProvider = dp;
      }

       

      when the XML is structed like this, it works great.

      <list>

          <APR>

              <detail Date="04/01/09" Credit="0.00" Debit="494.00" Master="Bank of America" Desc="FiOS" />
              <detail Date="04/10/09" Credit="0.00" Debit="154.00" Master="Bank of America" Desc="Outback Steakhouse" />
          </APR>
      </list>

       

      if i change the XML to this...

      <list>

           <rep>

               <APR>

                  <detail Date="04/01/09" Credit="0.00" Debit="494.00" Master="Bank of America" Desc="FiOS" />
                 <detail Date="04/10/09" Credit="0.00" Debit="154.00" Master="Bank of America" Desc="Outback" />
               </APR>

           </rep>
      </list>

       

      and the XMLList var to this...

      var testList:XMLList = xmlDP.rep.APR.children();

       

      it does not work.  i've tried using various combinations of children, chilNodes, etc but obviously i'm not hitting the right code!  any help would be greatly appreciated as i need what little sanity i have left.  Thank You!

        • 1. Re: DataGrid with XML child nodes in AS3
          tsuyoshi.suzuki

          I had a same problem!

          I used example from below.

           

          http://interactivemultimedia.wordpress.com/2008/02/07/sample-flash-as3-code-to-help-design ers-and-developers-handle-data-and-communication/

           

          I hope this works for ya!

          //------------------------------------------------------

           

          import fl.controls.DataGrid;
          import fl.data.DataProvider;

           

          // drag&drop DataGrid component. name it as "myDataGrid". width should be appropriate


          var myRequest:URLRequest;
          var myLoader:URLLoader;

           

          myRequest = new URLRequest();
          myRequest.url='sortTest3.xml';
          myLoader = new URLLoader();
          myLoader.load(myRequest);
          myLoader.addEventListener(Event.COMPLETE, dataLoad);

           


          function dataLoad(e:Event):void {

           

              var dataXML:XML = XML(e.target.data);

           

              var myDataProvider:Array = [];
              var Date:String;
              var Credit:String;
              var Debit:String;
              var Master:String;
              var Desc:String;
             
              for each (var some:XML in dataXML.rep.APR.detail) {
                 
                  Date = some.@Date;
                  Credit = some.@Credit;
                  Debit = some.@Debit;
                  Master = some.@Master;
                  Desc = some.@Desc;

           

                  myDataProvider.push({Date:Date, Credit:Credit, Debit:Debit, Master:Master, Desc:Desc});
              }
              myDataGrid.columns = ["Date", "Credit", "Debit", "Master", "Desc"];
              myDataGrid.dataProvider = new DataProvider(myDataProvider);
              myDataGrid.getColumnAt(0).headerText = "Date";
              myDataGrid.getColumnAt(0).width = 90;
              myDataGrid.getColumnAt(1).headerText = "Credit";
              myDataGrid.getColumnAt(1).width = 90;
              myDataGrid.getColumnAt(2).headerText = "Debit";
              myDataGrid.getColumnAt(2).width = 90;
              myDataGrid.getColumnAt(3).headerText = "Master";
              myDataGrid.getColumnAt(3).width = 90;
              myDataGrid.getColumnAt(4).headerText = "Desc";
              myDataGrid.getColumnAt(4).width = 90;

           


          }

           

          //-------------sortTest3.xml---------

           

          <list>

               <rep>

                   <APR>

                      <detail Date="04/01/09" Credit="0.00" Debit="494.00" Master="Bank of America" Desc="FiOS" />
                     <detail Date="04/10/09" Credit="0.00" Debit="154.00" Master="Bank of America" Desc="Outback" />
                   </APR>

               </rep>
          </list>