6 Replies Latest reply on Nov 16, 2009 4:41 AM by diditin

    arraycollection into XMLlistcollection

    diditin

      Hi all,

       

      I am trying to create my xmllistcollection since days, and getting soo unsuccessful each time...

       

      I have a flat simple arraycollection like :

       

      id_orderclient_nameorders_name
      1client_name 1order_name 1
      2client_name 2order_name 2
      3client_name 3order_name 3
      4client_name 1order_name 4

       

      Objective: I want to display the above data in a tree, group by client_name.

       

       

      Please can you help me to create that xmllistcollection ?

       

      Thanks

       

      ps: what i have done so far:

       

       

       

       

      [Bindable]
      private var myXMLlist:XML = <clients></clients>;
      
      [Bindable]
      private var XMListcol:XMLListCollection;
      
      
      
      private function createXML():void
      {
           XMListcol = new XMLListCollection;
           var clientNamePrev:String;
           
           for (var i:int = 0; i < allOrders_array.length; i++)
           {
                var ordName:String = allOrders_array.getItemAt(i).orders_name;
                var idOrders:String = allOrders_array.getItemAt(i).id_orders;
                var clientName:String = allOrders_array.getItemAt(i).client_name;
                
                var clientNameTag:XML = <clientname>{clientName}</clientname>;
                //Alert.show("idOrders:"+ idOrders+ "order name:"+ ordName);
                
                var new0rderTag:XML = <ordername id={idOrders}>{ordName}</ordername>;
                
                
                if(clientNamePrev == clientName)
                {
                     Alert.show("clientNameTag: "+ clientNameTag);
                     clientNameTag.appendChild(new0rderTag);     
                }
                else
                {
                     //Alert.show("ord Prev: "+clientNamePrev + " orderName: "+ clientName);     
                     myXMLlist = myXMLlist.appendChild(clientNameTag);
                }
           
                clientNamePrev= clientName;
           }
           
           XMListcol = new XMLListCollection(myXMLlist.children());
           Alert.show("listxml: "+ myXMLlist.descendants());
      
      }
      
      
        • 1. Re: arraycollection into XMLlistcollection
          Flex harUI Adobe Employee

          What would you want the XML representation to look like?

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: arraycollection into XMLlistcollection
            diditin Level 1

            Hi Mr. Alex,

             

            Thank you for replying me. My XML should probably look like the below format:

             

            <clients>
            
                <clientname name = 1>
            
                      <ordername>X </ordername>
            
                      <ordername>Y </ordername>
            
                </clientname>
            
                <clientname name = 2 >
            
                      <ordername> Z </ordername>
            
                </clientname>
            
                <clientname name = 3>
            
                      <ordername>A </ordername>
            
                </clientname>
            
            </clients>
            
            
            

            So that i have a tree like:

             

            -1
            -->X
            -->Y
            -2
            -->Z
            -3
            -->A
            

             

            All orders having the same client name should be regrouped.

             

            thank you

            • 3. Re: arraycollection into XMLlistcollection
              Flex harUI Adobe Employee

              I think you want to sort the AC by clientName first.  You seem to be on the right track otherwise.

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui

              1 person found this helpful
              • 4. Re: arraycollection into XMLlistcollection
                diditin Level 1

                Hi

                 

                Do you know how could i access the attribute 'id' from the tag '<ordername id =xx>'?

                 

                please find below the solution to my difficulties:

                 

                private function createXML():void
                {
                     var myXMLlist:XML = <clients></clients>;
                     var clientNamePrev:String = "";
                     var clientNameTag:XML;
                     //Alert.show("arraylength : " +allOrders_array.getItemAt(20).client_name );
                     for (var i:int = 0; i < allOrders_array.length; i++)
                     {
                          var ordName:String = allOrders_array.getItemAt(i).orders_name;
                          var idOrders:String = allOrders_array.getItemAt(i).id_orders;
                          var clientName:String = allOrders_array.getItemAt(i).client_name;
                               
                          var new0rderTag:XML = <ordername id={idOrders} label={ordName}/>;                         
                          
                          if (clientNamePrev == "")
                          {
                               clientNameTag = <clientname label={clientName}></clientname>;
                               clientNameTag.appendChild(new0rderTag);                         
                          }
                          else if(clientNamePrev == clientName)
                          {
                               clientNameTag.appendChild(new0rderTag);
                               //Alert.show("clientNameTag: "+ clientNameTag);
                          }
                          else if(clientNamePrev != clientName)
                          {
                               //Alert.show("ord Prev: "+clientNamePrev + " orderName: "+ clientName);     
                               myXMLlist = myXMLlist.appendChild(clientNameTag);
                               clientNameTag = <clientname label={clientName}></clientname>;
                               clientNameTag.appendChild(new0rderTag);                         
                          }
                          else
                          {
                               Alert.show("Please contact Administrator @ yy@xx.com", "Alert");
                          }
                          
                          if (i == (allOrders_array.length - 1))
                          {
                               myXMLlist = myXMLlist.appendChild(clientNameTag);
                          }
                          
                          clientNamePrev= clientName;
                     }
                     
                     XMListcol = new XMLListCollection(myXMLlist.children());
                     //Alert.show("listxml:" + XMListcol); 
                
                }
                

                 

                 

                Thank you

                • 5. Re: arraycollection into XMLlistcollection
                  Flex harUI Adobe Employee

                  Attributes are accessed via "@" as in someTag.@id

                   

                  Alex Harui

                  Flex SDK Developer

                  Adobe Systems Inc.

                  Blog: http://blogs.adobe.com/aharui

                  • 6. Re: arraycollection into XMLlistcollection
                    diditin Level 1

                    Yeah, it's quite different from the way we usually do while accessing arraycollection. XMLListCollection proceeds as follows:

                    Eg of my XML:
                    
                    <clients>
                         <clientname label=CLIENTNAME>
                              <ordername id=12332 label=MYORDERNAME1/>
                              <ordername id=53423 label=MYORDERNAME2/>
                                etc..
                         </clientname>
                         etC..
                    </clients>
                    
                     private function disactiveOrder(evt:CloseEvent):void
                         {
                                   //GET XML NODE FIRST. "list_orders" is the name of my TREE
                              var node:XML = list_orders.selectedItem as XML;
                                   //IF USERS ACCEPTS CONFIRMATION 
                              if (evt.detail == Alert.OK)
                              {
                              //call function
                              AMF_orders.disactiveOrder.send(Number(node.@id));
                         }          
                         }