4 Replies Latest reply on Dec 17, 2007 1:54 PM by binkerboy

    ArrayCollection to XML question.

    binkerboy
      Hi, all. I am new both here and to using FLEX, having been a CF coder since it first came out. Also, please excuse the length of this post, but I may reference in the future. And... thanks for the all the great posts so far, this really jumpstarted me.

      Currently, most of my code is "test", learning how to make FLEX do what I want. I am working on a project in which I need to put data retrieved from a MS SQL Server into an AdvancedDataGrid (ADG). The data is such that there may be many children (and their children, etc.) for a given request. What I want to do is return the data a bit at a time, so that on the first call I get all the top-level parents (PKIDs). This might be 10 to many hundred records.

      This is done by the user selecting from two comboBoxes, which then initiates a RemoteObject call and hands back a data set to the function below. The function converts the array into XML so I can then have a Hierarchy in which to insert children later. Right now, to show the itemOpen arrow, I add a dummy child record to each parent (I will find a better way later). When the user clicks on the itemOpen arrow, I currently go and get that records children, using the PKID, which currently works.

      My problem is that, in a second, similar function, when I get the children and create mainTreeChildren, I want to put them in XML format (which I do), then insert them into the mainTree XML at the appropriate parent, then open the datagrid back to that location. This is where I break down. I get a message in the function to make the children that:

      1120: Access of undefined property mainTree.

      So, even though I created the XML in the function below, when I try to access it in another function, I get the error above.

      Frankly, I am just starting to try to figure out how I will then insert the XML children, but that can come later. I have also included an example of the XML created in this function. Full code available if needed.

      /* Now lets get the data for the main adgHierarchies Advanced Data Grid. */
      [Bindable]
      private var adgResultsArray:ArrayCollection;

      private function getNodesByRelationHandler(adg:ResultEvent):void {
      var temp:ArrayCollection = adg.result as ArrayCollection;
      adgResultsArray = new ArrayCollection(temp.toArray());

      //convert to XML
      var mainTree:XML = new XML("<HIERARCHY NAME = '" + dgHierarchies.selectedItem.NAME + "' PKID = '" + dgHierarchies.selectedItem.PKID + "' DESCRIPTION = '" + dgHierarchies.selectedItem.DESCRIPTION + "'></HIERARCHY>");

      for (var i:int = 0; i < adgResultsArray.length; i++)
      {
      var newnode:XML = new XML();
      newnode =

      <node NAME={adgResultsArray .NAME}
      PKID={adgResultsArray
      .PKID}
      DESCRIPTION={adgResultsArray .DESCRIPTION}
      RANGEID={adgResultsArray
      .RANGEID}
      TYPE={adgResultsArray .TYPE}
      RELATIONID={adgResultsArray
      .RELATIONID}
      MASS="100" WEIGHT="150" LENGTH="40">

      <node NAME="1 - TestMakeChildPlaceHolder" PKID="0"
      DESCRIPTION="This causes the little arrow for the item opener to show."></node>

      </node>;

      mainTree = mainTree.appendChild(newnode);

      }

      adgHierarchy.dataProvider = new HierarchicalData(mainTree);
      rawXML.text = mainTree.toXMLString(); //see the XML in a textbox
      adgHierarchy.validateNow();
      callLater(expandFirstNode);

      /* called by callLater to ensure that the tree is ready to have expandItem called on it*/
      function expandFirstNode():void
      {
      adgHierarchy.expandItem(mainTree, true); //expand the node
      }
      }

      Example XML

      <HIERARCHY NAME="Gary Test Hierarchy" PKID="2684" DESCRIPTION="This is a listing of the Lander something mission">
      <node NAME="Fuel Guage - 3.5 in" PKID="2690" DESCRIPTION="" RANGEID="2690" TYPE="0" RELATIONID="601" MASS="100" WEIGHT="150" LENGTH="40">
      <node NAME="1 - TestMakeChildPlaceHolder" PKID="0" DESCRIPTION="This causes the little arrow for the item opener to show."/>
      </node>
      <node NAME="Speedometer and Odometer" PKID="2708" DESCRIPTION="" RANGEID="2708" TYPE="0" RELATIONID="601" MASS="100" WEIGHT="150" LENGTH="40">
      <node NAME="1 - TestMakeChildPlaceHolder" PKID="0" DESCRIPTION="This causes the little arrow for the item opener to show."/>
      </node>
      </HIERARCHY>

      Gary
        • 1. Re: ArrayCollection to XML question.
          ntsiii Level 3
          What line is producing the error?
          Tracy
          • 2. Re: ArrayCollection to XML question.
            binkerboy Level 1
            Hi. Thanks for the reply. It was not in the post. I wanted to post all my code, so people could see it in context, but it is too big. Also, I have now changed the code. The gist of the problem is that I created the mainTree XML in the function getNodesByRelationHandler, but when I tried to use it, even just to display in textbox in another function called getNodesByRelationHandler2, which is called when users click on the itemOpener, I get the error message. If you like, I can send the whole code via email, but I know your time is important. Thanks, again.
            • 3. Re: ArrayCollection to XML question.
              binkerboy Level 1
              quote:

              Originally posted by: ntsiii
              What line is producing the error?
              Tracy


              Hi. Ok, below is a simple version of my bigger app. In this, I generate the XML on the fly. If you run this, it works. However, if you uncomment the line:

              //txtChildPositionXML.text = mainTree.node.(attribute('PKID') == evt.item.@PKID.toString());

              you get the error: 1120: Access of undefined property mainTree.

              Is this a scoping error. On top of that, I am not even sure about the rest of the line. Thanks

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initMakeXML()" width="1000" height="1000">

              <mx:Script>
              <![CDATA[

              import mx.collections.HierarchicalData;
              import mx.utils.ArrayUtil
              import mx.utils.ObjectUtil;
              import mx.collections.*;
              import mx.collections.ArrayCollection
              import mx.collections.IViewCursor
              import mx.controls.Alert
              import mx.core.UIComponent;
              import mx.events.ListEvent
              import mx.events.CollectionEvent;
              import mx.events.CollectionEventKind;
              import mx.events.AdvancedDataGridEvent
              import mx.rpc.events.FaultEvent;
              import mx.rpc.events.ResultEvent;

              private function initMakeXML():void {
              //lets make some parent XML
              var mainTree:XML = new XML("<HIERARCHY NAME = 'Test Parent'></HIERARCHY>");
              var PKIDcount:int = 99
              var PKIDcount1:int = 299
              var PKIDcount2:int = 499

              for (var i:int = 0; i < 100; i++)

              {
              PKIDcount = (PKIDcount + 1)
              PKIDcount1 = (PKIDcount1 + 1)
              PKIDcount2 = (PKIDcount2 + 1)
              var newnode:XML = new XML();
              newnode =

              <node NAME={"Parent " + i} PKID={PKIDcount} MASS="100" WEIGHT="150" LENGTH="40">

              <node NAME="1 - TestMakeChildPlaceHolder" PKID={PKIDcount1}
              DESCRIPTION="This causes the little arrow for the item opener to show.">

              <node NAME="2 - TestMakeChildPlaceHolder" PKID={PKIDcount2}
              DESCRIPTION="This causes the little arrow for the item opener to show.">
              </node>

              </node>

              </node>;

              mainTree = mainTree.appendChild(newnode);

              }

              adgHierarchy.dataProvider = new HierarchicalData(mainTree);
              rawXML.text = mainTree.toXMLString();
              adgHierarchy.validateNow();
              callLater(expandFirstNode);

              /* called by callLater to ensure that the tree is ready to have expandItem called on it*/
              function expandFirstNode():void
              {
              adgHierarchy.expandItem(mainTree, true); //expand the node
              }
              }

              public var alreadyOpened:Array = new Array;
              public var alreadyOpenedAC:ArrayCollection = new ArrayCollection(alreadyOpened);

              private function addChildXML(evt:AdvancedDataGridEvent):void {
              var containsPKID:Boolean = alreadyOpenedAC.contains(evt.item.@PKID.toString()); //lets see if the PKID is in the array

              if (containsPKID == false) { //if it contains the PKID, skip running the code below

              alreadyOpenedAC.addItemAt(evt.item.@PKID.toString(), 0); //if we do open the tree item, add the PKID so we will skip opening it next time
              txtPKID.text = alreadyOpenedAC.toString() //view the array as it grows
              //txtChildPositionXML.text = mainTree.node.(attribute('PKID') == evt.item.@PKID.toString());


              }

              }


              ]]>
              </mx:Script>

              <mx:AdvancedDataGrid width="900" height="291" editable="true"
              id="adgHierarchy" x="6.5" y="10" itemOpen="addChildXML(event)"
              >
              <mx:columns>
              <mx:AdvancedDataGridColumn dataField="@NAME"
              headerText="Name" editable="true" width="300" />
              <mx:AdvancedDataGridColumn dataField="@DESCRIPTION"
              headerText="Description" editable="true"/>
              <mx:AdvancedDataGridColumn dataField="@PKID"
              headerText="PKID" editable="false"/>
              <mx:AdvancedDataGridColumn dataField="@MASS"
              headerText="Mass" editable="false"/>
              <mx:AdvancedDataGridColumn dataField="@WEIGHT"
              headerText="Weight" editable="false"/>
              <mx:AdvancedDataGridColumn dataField="@LENGTH"
              headerText="Length" editable="false"/>
              </mx:columns>
              </mx:AdvancedDataGrid>

              <mx:Label x="10" y="309" text="mainTree XML" color="#FFFFFF" fontStyle="normal" fontWeight="bold"/>
              <mx:TextArea x="10" y="338" height="90" id="rawXML" width="900"/>

              <mx:Label x="10" y="453" text="mainTreeChildren XML" color="#FFFFFF" fontStyle="normal" fontWeight="bold"/>
              <mx:TextArea x="10" y="479" height="90" id="rawXML0" width="900"/>

              <mx:Label x="10" y="577" text="Child Position in XML" color="#FFFFFF" fontWeight="bold"/>
              <mx:TextArea x="10" y="603" height="23" id="txtChildPositionXML"/>

              <mx:Label x="10" y="634" text="Child Position in Grid" color="#FFFFFF" fontWeight="bold"/>
              <mx:TextArea x="10" y="660" height="23" id="txtChildPositionGrid"/>

              <mx:Label x="10" y="691" color="#FFFFFF" fontWeight="bold" text="PKIDs"/>
              <mx:TextArea x="10" y="717" height="54" id="txtPKID" width="511"/>
              </mx:Application>
              • 4. Re: ArrayCollection to XML question.
                binkerboy Level 1
                Ok, I figure it out. Silly me. I had to make the mainTree XML Bindable. So, it looks like this:

                (first bit of code, then)

                [Bindable]
                public var mainTree:XML;

                private function initMakeXML():void {
                //lets make some parent XML
                mainTree = new XML("<HIERARCHY NAME = 'Test Parent'></HIERARCHY>");

                (rest of code)