3 Replies Latest reply on Mar 19, 2009 8:30 PM by Newsgroup_User

    Categories/Subcategories Tree Component

    released87
      Hi,

      I am creating a tree component from a products category/subcategory properties.

      I am using an XML product list structured as follows:

      <products>
      <product>
      <category>Category 1</category>
      <subcategory>Sub 1</sub>
      <name>Name</name>
      <number>325</number>
      </product>
      <products>

      The product node will be repeating. What I intend to do is load the xml, creating an array/xml collection with the categories. Then I need to remove duplicates, because lots of products will share the same category.

      Total:
      <category>Category 1</category>
      <category>Category 2</category>
      <category>Category 3</category>
      <category>Category 1</category>

      Duplicates Removed:
      <category>Category 1</category>
      <category>Category 2</category>
      <category>Category 3</category>

      After that I need to loop through the products again, using each category outputted from the previous list, and merge into a new xml file.
      for(subcategory in categoryArray){
      categoryList.(category== category1).addElement(subcategory);
      }

      Basically I need the output to be something like this:
      <category name="Category 1">
      <sub>Sub1</sub>
      <sub>Sub2</sub>
      </category>
      <category name="Category 2">
      <sub>Sub 1</sub>
      <sub>Sub 3</sub>
      </category>

      That way I can use the outputted XML in a tree control to select categories and then subcategories if needed.

      I know this is an advanced process, so if you could just point in the right direction or some examples that would be greatly appreciated.

      Jesse
        • 1. Re: Categories/Subcategories Tree Component
          Level 7

          "released87" <webforumsuser@macromedia.com> wrote in message
          news:gpov9k$rdp$1@forums.macromedia.com...
          > Hi,
          >
          > I am creating a tree component from a products category/subcategory
          > properties.
          >
          > I am using an XML product list structured as follows:
          >
          > <products>
          > <product>
          > <category>Category 1</category>
          > <subcategory>Sub 1</sub>
          > <name>Name</name>
          > <number>325</number>
          > </product>
          > <products>
          >
          > The product node will be repeating. What I intend to do is load the xml,
          > creating an array/xml collection with the categories. Then I need to
          > remove
          > duplicates, because lots of products will share the same category.
          >
          > Total:
          > <category>Category 1</category>
          > <category>Category 2</category>
          > <category>Category 3</category>
          > <category>Category 1</category>
          >
          > Duplicates Removed:
          > <category>Category 1</category>
          > <category>Category 2</category>
          > <category>Category 3</category>

          http://joshblog.net/2007/05/17/filter-xml-data-with-e4x-in-flash-part-2/

          You'll need to copy the nodes rather than just put a reference to them in
          there.

          HTH;

          Amy


          • 2. Re: Categories/Subcategories Tree Component
            released87 Level 1
            how can i append multiple children to a node?

            var outputXML:XML= new XML(<output></output>);
            //Categories
            categoryList= productsXML.data.row.category;
            var testList:XMLList= productsXML.data.row.subcategory;
            var testString:XML= <test></test>;
            testString.test.appendChild(testList.toXMLString());
            for(var i:int =1; i<categoryList.length(); i++){
            var temp:String= productsXML.data.row.(id== i).category;
            outputXML.appendChild(<category label={temp}>{testString}</category>);
            }
            var hash:Object = {};
            var uniqueList:XMLList = outputXML.*.(hash[toXMLString()] == undefined ? hash[toXMLString()] = valueOf() : false);

            this is a testing script, but I keep getting the same error because I cannot use appendChild() to add multiple nodes.

            TypeError: Error #1086: The appendChild method only works on lists containing one item.

            • 3. Re: Categories/Subcategories Tree Component
              Level 7

              "released87" <webforumsuser@macromedia.com> wrote in message
              news:gptm9n$po0$1@forums.macromedia.com...
              > how can i append multiple children to a node?
              >
              > var outputXML:XML= new XML(<output></output>);
              > //Categories
              > categoryList= productsXML.data.row.category;
              > var testList:XMLList= productsXML.data.row.subcategory;
              > var testString:XML= <test></test>;
              > testString.test.appendChild(testList.toXMLString());
              > for(var i:int =1; i<categoryList.length(); i++){
              > var temp:String= productsXML.data.row.(id== i).category;
              > outputXML.appendChild(<category label={temp}>{testString}</category>);

              ^loop through these nodes and make copies of them.