8 Replies Latest reply on Jun 24, 2010 7:49 AM by rtalton

    Need assistance filtering E4X stored in XMLListCollection

    PeakDigital Level 1

      I store my data provider for a tree control in a separate class, in an XMLListCollection. When it is gathered from the database, the HTTPService has a resultFormat of e4x, and the result is copied to my XMLListCollection.

       

      Now, I want to grab a list of nodes that have an attribute of isOpen == true. I found these two references that make it appear easy but they aren't working for me:

      http://blog.flexexamples.com/2008/01/15/expanding-nodes-in-a-flex-tree-control-using-the-o penitems-property/

      http://joshblog.net/2007/05/08/methods-to-filter-data-with-e4x-in-flash-9/

       

      I am wondering if it is because my XMLListCollection does not use consistent names for all levels of nodes.

       

      This code

       

      var nodeTest:XMLList=_modelListManagerTreeData.xmlTreeData..@isBranch; 

       

      shows an error in the development environment of "1119: Access of possibly undefined property isBranch through a reference with static type mx.collections:XMLListCollection."

       

      Is it not possible to run E4X syntax on items stored in another class, or what am I doing wrong?

       

      Sample XML I am working with, showing varying node names. In my method, top-level nodes are named "parentNode" and the internal nodes could be either "equipment" or "item" depending on if they are a branch or a leaf.

       

      <parentNode ID="2" Title="Hand Tools" NodeType="root" isBranch="true" isOpen="false">
        <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="2" isOpen="false"/>
        <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="2" isOpen="false"/>
      </parentNode>
      <parentNode ID="4" Title="Power Tools" NodeType="root" isBranch="true" isOpen="false">
        <equipment ID="10" Title="Hydraulics" NodeType="subcat" isBranch="true" ParentID="4" isOpen="false">
          <item ID="8" Title="Tool 1" NodeType="item" isBranch="false"/>
          <item ID="9" Title="Tool 2" NodeType="item" isBranch="false"/>
          <item ID="10" Title="Tool 3" NodeType="item" isBranch="false"/>
          <item ID="11" Title="Tool 4" NodeType="item" isBranch="false"/>
          <item ID="12" Title="Tool 5" NodeType="item" isBranch="false"/>
          <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="10" isOpen="false"/>
          <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="10" isOpen="false"/>
        </equipment>
        <equipment ID="11" Title="Electric, Cordless" NodeType="subcat" isBranch="true" ParentID="4" isOpen="false">
          <item ID="28" Title="9v screwdriver" NodeType="item" isBranch="false"/>
          <equipment ID="12" Title="12V" NodeType="subcat" isBranch="true" ParentID="11" isOpen="false">
            <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="12" isOpen="false"/>
            <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="12" isOpen="false"/>
          </equipment>
          <equipment ID="13" Title="18V" NodeType="subcat" isBranch="true" ParentID="11" isOpen="false">
            <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="13" isOpen="false"/>
            <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="13" isOpen="false"/>
          </equipment>
          <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="11" isOpen="false"/>
          <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="11" isOpen="false"/>
        </equipment>
      </parentNode>
      

       

      Thank you for your time.

       

      Paul

        • 1. Re: Need assistance filtering E4X stored in XMLListCollection
          PeakDigital Level 1

          I haven't found a solution for this yet. Can anyone point me in the right direction?

           

          Thanks.

          Paul

          • 2. Re: Need assistance filtering E4X stored in XMLListCollection
            pradeep reddy Level 1

            Hi paul,

             

            Are you using DataDescriptor.. for your tree control..?

             

             

            Thanks,

            Pradeep

            • 3. Re: Need assistance filtering E4X stored in XMLListCollection
              PeakDigital Level 1

              Pradeep,

               

              Thank you for replying. No, I am not yet familiar with DataDescriptor.

               

              Paul

              • 4. Re: Need assistance filtering E4X stored in XMLListCollection
                BhaskerChari Level 4

                Hi PeakDigital,

                 

                You can do something like this ...Please find the code below:

                 

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();" layout="absolute">
                <mx:Script>
                  <![CDATA[
                   import mx.collections.ArrayCollection;
                   import mx.rpc.xml.SimpleXMLDecoder;
                  
                   private var acAllNodes:ArrayCollection;
                   private var acOpenNodes:ArrayCollection;
                  
                   private function init():void
                   {
                    convertXmlListToArrayCollection();
                    getListOfIsOpenNodes();
                   }
                   private function convertXmlListToArrayCollection():void
                   {
                    var strXML:String = dpNodes.toXMLString();
                    strXML = "<root>" + strXML + "</root>";
                    var xml:XML = new XML(strXML);
                    var xmlDoc:XMLDocument = new XMLDocument(xml);
                    var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
                    var resultObj:Object = decoder.decodeXML(xmlDoc);
                    acAllNodes = new ArrayCollection();
                   
                    if(resultObj.root.hasOwnProperty("parentNode"))
                    {
                     if(resultObj.root.parentNode is ArrayCollection)
                     {
                      acAllNodes = resultObj.root.parentNode;
                     }
                     else if(resultObj.root.parentNode is Object)
                     {
                      acAllNodes.addItem(resultObj.root.parentNode);
                     }
                    }   
                   }
                   // acOpenNodes contains the list of nodes which have isOpen = true on parentNode
                   // If you want to get the list of nodes which have isOpen = true on childNodes i.e; on equipment node(Loop through the equipment array collection within each item)
                   private function getListOfIsOpenNodes():void
                   {
                    acOpenNodes = new ArrayCollection();
                    for(var intLoop:int=0;intLoop<acAllNodes.length;intLoop++)
                    {
                     if(acAllNodes.getItemAt(intLoop).isOpen == true)
                     {
                      acOpenNodes.addItemAt(acAllNodes.getItemAt(intLoop),intLoop);
                     }
                    }
                   
                   }
                  ]]>
                </mx:Script>

                <mx:XMLListCollection id="dpNodes">
                        <mx:source>
                            <mx:XMLList>
                                <parentNode ID="2" Title="Hand Tools" NodeType="root" isBranch="true" isOpen="true">
                      <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="2" isOpen="true"/>
                      <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="2" isOpen="false"/>
                    </parentNode>
                    <parentNode ID="4" Title="Power Tools" NodeType="root" isBranch="true" isOpen="false">
                      <equipment ID="10" Title="Hydraulics" NodeType="subcat" isBranch="true" ParentID="4" isOpen="false">
                        <item ID="8" Title="Tool 1" NodeType="item" isBranch="false"/>
                        <item ID="9" Title="Tool 2" NodeType="item" isBranch="false"/>
                        <item ID="10" Title="Tool 3" NodeType="item" isBranch="false"/>
                        <item ID="11" Title="Tool 4" NodeType="item" isBranch="false"/>
                        <item ID="12" Title="Tool 5" NodeType="item" isBranch="false"/>
                        <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="10" isOpen="false"/>
                        <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="10" isOpen="false"/>
                      </equipment>
                      <equipment ID="11" Title="Electric, Cordless" NodeType="subcat" isBranch="true" ParentID="4" isOpen="false">
                        <item ID="28" Title="9v screwdriver" NodeType="item" isBranch="false"/>
                        <equipment ID="12" Title="12V" NodeType="subcat" isBranch="true" ParentID="11" isOpen="false">
                          <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="12" isOpen="false"/>
                          <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="12" isOpen="false"/>
                        </equipment>
                        <equipment ID="13" Title="18V" NodeType="subcat" isBranch="true" ParentID="11" isOpen="false">
                          <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="13" isOpen="false"/>
                          <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="13" isOpen="false"/>
                        </equipment>
                        <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="11" isOpen="false"/>
                        <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="11" isOpen="false"/>
                      </equipment>
                    </parentNode>
                            </mx:XMLList>
                        </mx:source>
                    </mx:XMLListCollection>

                </mx:Application>

                 

                If this post answers your question or helps, please kindly mark it as such.


                Thanks,

                Bhasker Chari

                1 person found this helpful
                • 5. Re: Need assistance filtering E4X stored in XMLListCollection
                  PeakDigital Level 1

                  Thank you for taking the time to post that code. I will go through it and see what I can do.

                   

                  Paul

                  • 6. Re: Need assistance filtering E4X stored in XMLListCollection
                    rtalton Level 4

                    E4X will work on XMLList, however, for XMLListCollection or ArrayCollection you must use the methods of the collections class for searching and filtering.

                    1 person found this helpful
                    • 7. Re: Need assistance filtering E4X stored in XMLListCollection
                      PeakDigital Level 1

                      Thank you.  So many details to learn about handling XML.

                      • 8. Re: Need assistance filtering E4X stored in XMLListCollection
                        rtalton Level 4

                        I thought I'd pass along some links which were helpful to me.

                         

                        This one uses a very good down-to-earth writing style:

                        http://www.morearty.com/blog/2007/03/13/common-e4x-pitfalls/

                         

                        Marc Bir is very knowledgeable on e4x. Who knows he may have invented it himself.

                        This one is a little advanced at times but includes very helpful project file examples.

                        http://allyourflex.wordpress.com/2008/08/19/360flex-e4x/

                         

                        Marc Bir presentation on Adobe TV. Extra geek sauce.

                        http://tv.adobe.com/watch/max-2008-develop/e4x-is-good4u-by-marc-bir/