4 Replies Latest reply: Jul 11, 2012 10:17 PM by Kristtee RSS

    XML data in Combo and List comps

    Kristtee Community Member

      Hi there!

      I am too new to scripting. Yet I am trying simple ones. I have fla with a ComboBox[mycb], a List Comp[mylist], and a Textarea[myta] on stage. I have a simple external xml file as below:

       

      My intension is to list the categories in combobox (distict lables). on selecting one, list comp lists all edibles under that category in combo. Then on selecting an edible in the list component, it just displays the content in the textarea.

       

      --------------

      edibles.xml

      ----------------

      <?xml version="1.0" encoding="UTF-8"?>

      <edibles>

                <item id="1">

                          <cate>Fruit</cate>

                          <nam>Apple</nam>

                </item>

                <item id="2">

                          <cate>Fruit</cate>

                          <nam>Pine Apple</nam>

                </item>

                <item id="3">

                          <cate>Fruit</cate>

                          <nam>Wood Apple</nam>

                </item>

                <item id="4">

                          <cate>Fruit</cate>

                          <nam>Custard Apple</nam>

                </item>

                <item id="5">

                          <cate>Vegetable</cate>

                          <nam>Bitter Groud</nam>

                </item>

                <item id="6">

                          <cate>Vegatable</cate>

                          <nam>Carrot</nam>

                </item>

                <item id="7">

                          <cate>Vegetable</cate>

                          <nam>Pumkin</nam>

                </item>

                <item id="8">

                          <cate>Vegetable</cate>

                          <nam>Kale Greens</nam>

                </item>

      </edibles>

      ------------------------------

      here is my as3 code

      --------------------------

      import flash.net.URLLoader;

      import flash.events.Event;

      import flash.net.URLRequest;

       

       

      var xmldata:XML = new XML();

      var cateAr:Array = new Array();

      var nameAr:Array = new Array();

      var loader:URLLoader = new URLLoader();

      loader.addEventListener(Event.COMPLETE, loadData);

      loader.load(new URLRequest("edibles.xml"));

       

       

      function loadData(e:Event):void{

                xmldata = new XML(e.target.data);

                //trace(xmldata..cate.length());

                for(var i:uint=0;i<xmldata..cate.length();i++){

                          cateAr.push(xmldata..cate[i]);

                          nameAr.push(xmldata..nam[i]);

                          mycb.addItem( { label: cateAr[i], data:i } );

                          mycb.addEventListener(Event.CHANGE, listit);

                }

                parseData(xmldata);

      }

       

       

      function parseData(nams:XML):void{

                var namlist:XMLList = nams.nam;

                for each(var nlist:XML in namlist){

                          mylist.addItem({label:nlist, data:nlist});

                }

      }

      function listit(e:Event):void{

                var no:Number = Number(mycb.selectedItem.data);

          trace(nameAr[no]);

      }

      -----------------------------------

      The problem I have is:

       

      1. ComboBox displayes all items. not distict items. I mean it lists all with duplicates.

      2. LIst component does not list any item on selecting a category in combo

      3. onselecting a combo item it outputs the item.

       

      Please help me. Appreciate your time and help.

      Thank you

      Kristtee

       

      here is the fla file:https://docs.google.com/open?id=0ByHOlDbL5njbdEp4bkdYQ2RpNFE

        • 1. Re: XML data in Combo and List comps
          Ned Murphy CommunityMVP

          The code does what you tell it to do.  The loop that you have for parsing the data is filling up the lists directly whereas what you want to do is manage the data a bit more before you store it, and then process it a bit more when you select it.

           

          You should store each set as an object, meaning you parse on the item node rather than the cate/nam nodes.  Store all of these objects in an array. 

           

          After that array is filled, go thru it and create another array of all of the different categories, using the Array.indexOf() method on this second array to determine if an item (cate) already exists in it so that you do not duplicate any.  Use this second array to fill the combobox.

           

          Then, when you select one of the items in the combobox, have your code search thru the first array for all objects that have the selected category and add them to the list component (probably add the entire object as the data element of each).

          • 2. Re: XML data in Combo and List comps
            Kristtee Community Member

            Hi

            I remember you and i have great respect for you. I do remember you have help me once year ago. I am thankful. Thank you again. I read out loud your explanation, yet I find it hard to get it. I am too dumb with coading. It took a more than two weeks to put up all that is already there. Now, as to your suggestion I am completely lost. Sorry. Yet I will give it a try. Thank you so much and I really appreciate your time and help. I know you are far too budy, Thank you again.

            • 3. Re: XML data in Combo and List comps
              Ned Murphy CommunityMVP

              To try to help you think thru what I offered, where you have the following lines in your loop....

               

                      cateAr.push(xmldata..cate[i]); 

                      nameAr.push(xmldata..nam[i]);

               

              I am saying you should store that data differently, as objects....

               

                      dataAr.push( { category: xmldata..cate[i], name:xmldata..nam[i], id: i } );

               

              and only store unique categories in the array cateAr and use it to fill the combobox...

               

                      if(cateAr.indexOf(xmldata..cate[i]) == -1){ 

                            cateAr.push(xmldata..cate[i]);

                      }

               

              ten, when you select an item from the combobox you loop thru the objects in the dataAr array to see which ones match the category to include in the list.

              • 4. Re: XML data in Combo and List comps
                Kristtee Community Member

                Hello!

                I try to do as you have suggested but I do not see it work right. I must have made some mistake. but it does not show up. too.

                 

                here is the modified code:

                ----------------------------------------

                import flash.net.URLLoader;

                import flash.events.Event;

                import flash.net.URLRequest;

                 

                 

                var xmldata:XML = new XML();

                var dataAr:Array = new Array();

                var cateAr:Array = new Array();

                var nameAr:Array = new Array();

                var loader:URLLoader = new URLLoader();

                loader.addEventListener(Event.COMPLETE, loadData);

                loader.load(new URLRequest("edibles.xml"));

                 

                 

                function loadData(e:Event):void{

                          xmldata = new XML(e.target.data);

                          //trace(xmldata..item.length()); // outputs 8

                for(var i:uint=0;i<xmldata..item.length();i++){

                                    dataAr.push( { category: xmldata..cate[i], name:xmldata..nam[i], id: i } );

                                    if(cateAr.indexOf(xmldata..cate[i]) == -1){

                              cateAr.push(xmldata..cate[i]);

                                                mycb.addItem( { label: cateAr[i], data:i } );

                        }

                                    nameAr.push(xmldata..nam[i]);

                                    mycb.addEventListener(Event.CHANGE, listit);

                          }

                          parseData(xmldata);

                }

                 

                 

                function parseData(nams:XML):void{

                          var namlist:XMLList = nams.nam;

                          for each(var nlist:XML in namlist){

                                    mylist.addItem({label:nlist, data:nlist});

                          }

                }

                function listit(e:Event):void{

                          var no:Number = Number(mycb.selectedItem.data);

                    trace(nameAr[no]);

                }