20 Replies Latest reply on Apr 30, 2010 11:01 AM by PopetMaster

    xml problems

    PopetMaster

      hello!, I actually been working about a mini-form, it is have a 3 combobox and one textbox components.
      The basic idea basica is load a country series, each one of this have a state and each state have a city, also every city has a price.

       

      Right now its working only the 3  comboboxes but no the textbox whit the price.

       

      The structure of my XML is:
      ------------------------------------------------------------------------------------------ -----
      <countries>
      <country label="COUNTRY-ONE">
      <state  label="STATE-A">
      <city label="CITY-1A"  price="123"/>
      <city label="CITY-2A" price="456"/>
      </state>
      <state  label=" STATE-B ">
      <city label="CITY-1B" price  ="789"/>
      </state>
      <state label=" STATE-C  ">
      <city label="CITY-1C" price="567"/>
      </state>
      </country>

       

      <country  label="COUNTRY-TWO">
      <state label=" STATE-1">
      <city  label="CITY-A1 " price="654"/>
      <city label="CITY-A2 "  price="321"/>
      </state>
      <state label=" STATE-2">
      <city label="CITY-B1" price="765"/>
      <city  label=" CITY-B2 " price="432"/>
      </state>
      </country>
      </countries>
      ------------------------------------------------------------------------------------------ ---------------
      ****************************************************************************************** ***************

      And the AS2 code that I been use is:
      ****************************************************************************************** ****************
      ------------------------------------------------------------------------------------------ ------------------

      var la_lista:XML = new XML();
      la_lista.ignoreWhite = true;
      la_lista.onLoad = function(ok){
      if(ok) show_countries(this.firstChild.childNodes);
      }
      la_lista.load('la_lista.xml');
      //------------------------------------------------------------------------------->
      var show_countries:Function = function(list_countries:Array):Void{
      var myprovider:Array = new Array();
      combo1.dataProvider = myprovider;
      myprovider.addItem({label:'- country -',data:'cone'});
      for(n=0;n<list_countries.length;n++){
      myprovider.addItem({label:list_countries[n].attributes['label'],data:n});
      }
      }
      //---------------------------------------------------------------------------------------- --------------->
      var show_states:Function = function(position_country:Number):Void{
      var list_states:Array = la_lista.firstChild.childNodes[position_country].childNodes;
      var myprovider:Array = new Array();
      combo2.dataProvider = myprovider;
      myprovider.addItem({label:'- state -',data:'ctwo'});
      for(n=0;n<list_states.length;n++){
      myprovider.addItem({label:list_states[n].attributes['label'],data:n});
      }
      }
      //---------------------------------------------------------------------------------------- --->
      var show_cities:Function = function(position_country:Number,position_state:Number):Void{
      var list_cities:Array = la_lista.firstChild.childNodes[position_country].childNodes[position_state].childNodes;
      var myprovider:Array = new Array();
      combo3.dataProvider = myprovider;
      myprovider.addItem({label:'- city -',data:'cthree'});
      for(n=0;n<list_cities.length;n++){
      myprovider.addItem({label:list_cities[n].attributes['label'],data:n});
      }


      }

       

      //---------------------------------------------------------------------------------------- --------->
      var countrywasselected:Object = new Object();
      countrywasselected.change = function(referencia){
      price.removeAll();
      combo2.removeAll();
      combo3.removeAll();
      if(referencia.target.value != '') show_states(referencia.target.value);
      }

       


      var statewasselected:Object = new Object();
      statewasselected.change = function(referencia){
      price.removeAll();
      combo3.removeAll();
      if(referencia.target.value != '') show_cities(combo1.value,referencia.target.value);
      }

       


      var citywasselected:Object = new Object();
      citywasselected.change = function(referencia){
      if(referencia.target.value != '') show_price(combo2.value,referencia.target.value);
      }

       


      combo1.addEventListener('change',countrywasselected);
      combo2.addEventListener('change',statewasselected);
      combo3.addEventListener('change',citywasselected);
      combo3.dataProvider = miDataProvider;

       


      stop();
      ------------------------------------------------------------------------------------------ -----------------------------------------------

       

      I try to follow the same structure to  load the price in the text box but doesn´t work, then I notice that it was  not void what should use but a string.

      Then  I try  change it and use a similar line:

       

      var show_price:Function =   function(label_country:String,label_state:String,label_city:String):String{

       

      But Doesn´t work, also gave me a error.

      I looking to load the price data on the textbox,  What can I do? I get really confused.., what´s wrong? What do you suggest me to do?

       

      Thanks.

        • 1. Re: xml problems
          kglad Adobe Community Professional & MVP

          where are you trying to parse the price?

          • 2. Re: xml problems
            PopetMaster Level 1

            Through  the following function:

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

            var show_price:Function = function(position_country:Number,position_state:Number,position_city:Number):Void{

            var list_price:Array = la_lista.firstChild.childNodes[position_country].childNodes[position_state].childNodes[po sition_city].childNodes;
            var myprovider:Array = new Array();
            price.dataProvider = myprovider;
            myprovider.addItem({label:list_cities[n].attributes['price'],data:n});

            }

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

             

            I try  changing the first line for this one,

            It is here when I  get an error (It doesn't work...)
            ----------------------------------------------------------------------------------------- -

            var  show_price:Function =  function(position_country:String,position_state:String,position_city:String):String{

            .

            .

            .

            }

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

             

            • 3. Re: xml problems
              kglad Adobe Community Professional & MVP

              i don't see that in your first message.  is it in there?

              • 4. Re: xml problems
                PopetMaster Level 1

                Sorry.. I wrote only the code that is working, here my full code:

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

                 

                var la_lista:XML = new XML();
                la_lista.ignoreWhite = true;
                la_lista.onLoad  = function(ok){
                if(ok) show_countries(this.firstChild.childNodes);
                }
                la_lista.load('la_lista.xml');
                //------------------------------------------------------------------------------  ->
                var show_countries:Function =  function(list_countries:Array):Void{
                var myprovider:Array = new  Array();
                combo1.dataProvider = myprovider;
                myprovider.addItem({label:'-  country -',data:'cone'});
                for(n=0;n<list_countries.length;n++){
                myprovider.addItem({label:list_countries[n].attributes['label'],data:n});
                }
                }
                //------------------------------------------------------------------------------  ------------------------->
                var show_states:Function =  function(position_country:Number):Void{
                var list_states:Array =  la_lista.firstChild.childNodes[position_country].childNodes;
                var  myprovider:Array = new Array();
                combo2.dataProvider = myprovider;
                myprovider.addItem({label:'-  state -',data:'ctwo'});
                for(n=0;n<list_states.length;n++){
                myprovider.addItem({label:list_states[n].attributes['label'],data:n});
                }
                }
                //------------------------------------------------------------------------------  ------------->
                var show_cities:Function =  function(position_country:Number,position_state:Number):Void{
                var  list_cities:Array =  la_lista.firstChild.childNodes[position_country].childNodes[position_state].chi  ldNodes;
                var myprovider:Array = new Array();
                combo3.dataProvider =  myprovider;
                myprovider.addItem({label:'- city -',data:'cthree'});
                for(n=0;n<list_cities.length;n++){
                myprovider.addItem({label:list_cities[n].attributes['label'],data:n});

                }
                }

                 

                //------------------------------------------------------------------------------   ------------------->

                var show_price:Function =  function(position_country:Number,position_state:Number,position_city:Number):Void{

                var list_price:Array =  la_lista.firstChild.childNodes[position_country].childNodes[position_state].chi  ldNodes[position_city].childNodes;
                var myprovider:Array = new  Array();
                price.dataProvider = myprovider;
                myprovider.addItem({label:list_cities[n].attributes['price'],data:n});

                }

                //------------------------------------------------------------------------------   ------------------->
                var countrywasselected:Object = new  Object();
                countrywasselected.change = function(referencia){
                price.removeAll();
                combo2.removeAll();
                combo3.removeAll();
                if(referencia.target.value  != '') show_states(referencia.target.value);
                }

                 


                var  statewasselected:Object = new Object();
                statewasselected.change =  function(referencia){
                price.removeAll();
                combo3.removeAll();
                if(referencia.target.value  != '') show_cities(combo1.value,referencia.target.value);
                }

                 


                var  citywasselected:Object = new Object();
                citywasselected.change =  function(referencia){
                if(referencia.target.value != '')  show_price(combo2.value,referencia.target.value);
                }

                 


                combo1.addEventListener('change',countrywasselected);
                combo2.addEventListener('change',statewasselected);
                combo3.addEventListener('change',citywasselected);
                combo3.dataProvider  = miDataProvider;

                 

                stop();

                • 5. Re: xml problems
                  kglad Adobe Community Professional & MVP

                  it looks like you're passing 2 parameters and you need to pass 3.

                  • 6. Re: xml problems
                    PopetMaster Level 1

                    As if? sorry, I´m a little new on it... I don´t get it...

                    • 7. Re: xml problems
                      kglad Adobe Community Professional & MVP

                      here's your show_price function:

                       

                      var show_price:Function =  function(position_country:Number,position_state:Number,position_city:Number):Void{

                       

                      var list_price:Array =  la_lista.firstChild.childNodes[position_country].childNodes[position_state].childNodes[po sition_city].childNodes;
                      var myprovider:Array = new  Array();
                      price.dataProvider = myprovider;
                      myprovider.addItem({label:list_cities[n].attributes['price'],data:n});

                       

                      }

                       

                      here's your show_price call:


                      show_price(combo2.value,referencia.target.value);

                       

                      your call should be passing 3 parameters.

                      • 8. Re: xml problems
                        PopetMaster Level 1

                        Got it. I change my show_price call for this:

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

                        show_price(combo2.value,combo1.value,referencia.target.value);

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

                         

                         

                        but nothing happens, also try so but still dosen`t show the price

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

                        show_price(combo1.value,combo2.value,referencia.target.value);

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

                        • 9. Re: xml problems
                          kglad Adobe Community Professional & MVP

                          use the trace() function to see if all 3 parameters are defined and expected values.

                          • 10. Re: xml problems
                            PopetMaster Level 1

                            Well guess not..

                             

                            I used

                            trace(price);    This brought me back:  _level0.price

                            trace(myprovider); This brought me back: [object Object]

                             

                            So I change this line:

                             

                            myprovider.addItem({label:list_cities[n].attributes['price'],data:n});

                             

                            for this one:

                             

                            myprovider.addItem({label:combo2.getSelectedItem().label.attributes['price'],data:n});

                             

                            but still,

                            trace(myprovider); brought me back: [object  Object]

                             

                            I don´t get it...

                            • 11. Re: xml problems
                              kglad Adobe Community Professional & MVP

                              what's the following trace() reveal:

                               

                              var  show_price:Function =  function(position_country:Number,position_state:Number,position_city:Number):Void

                              var list_price:Array =   la_lista.firstChild.childNodes[position_country].childNodes[position_state].childNodes[po sition_city].childNodes;

                              trace(list_cities[n].attributes['price']+" : "+n);

                              // i see a problem:  where is n defined?
                              var myprovider:Array = new   Array();
                              price.dataProvider = myprovider;
                              myprovider.addItem({label:list_cities[n].attributes['price'],data:n});

                               

                              }

                              • 12. Re: xml problems
                                PopetMaster Level 1

                                initially thought they would inherit n from show_cities function, after I saw this, It´s why try to change the line

                                 

                                myprovider.addItem({label:list_cities[n].attributes['price'],data:n});

                                 

                                for this one:
                                myprovider.addItem({label:combo2.getSelectedItem().label.attributes['price'],dat a:n});

                                 

                                with the idea of referring to the label value selected  in combo2, but it do nothing.

                                • 13. Re: xml problems
                                  kglad Adobe Community Professional & MVP

                                  is n defined?

                                   

                                  what's trace(n) show

                                  • 14. Re: xml problems
                                    PopetMaster Level 1

                                    Got it. n was not defined, adding some new lines to show_price  function

                                     

                                    for(n=0;n<list_price.length;n++){
                                        myprovider.addItem({label:list_price[n].attributes['price'],data:n});
                                    }

                                    trace(n);

                                     

                                     

                                    Now my show price function is It

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

                                    var show_price:Function = function(position_country:Number,position_state:Number,position_city:Number):Void{

                                    var list_price:Array = la_lista.firstChild.childNodes[position_country].childNodes[position_state].childNodes[po sition_city].childNodes;

                                     

                                    var myprovider:Array = new Array();
                                    price.dataProvider = myprovider;
                                    myprovider.addItem({label:combo3.getSelectedItem().attributes['price'],data:n});
                                    for(n=0;n<list_price.length;n++){
                                        myprovider.addItem({label:list_price[n].attributes['price'],data:n});
                                    }

                                    trace(n);
                                    }

                                     

                                    where,  trace(n);  returned me  the positional value  (I guess...) but not  value by name (the price itself)

                                    trace(n);        -------->   0

                                    • 15. Re: xml problems
                                      kglad Adobe Community Professional & MVP

                                      you need to pass n to your show_price() function.

                                      • 16. Re: xml problems
                                        PopetMaster Level 1

                                        how? I supposed the line was passing the n value, at  least for city selected...

                                         

                                        myprovider.addItem({label:combo3.getSelectedItem().attributes['price'],data:n});

                                        • 17. Re: xml problems
                                          kglad Adobe Community Professional & MVP

                                          when you call show_price(), you pass parameters used in the function definition:

                                           

                                          show_price("country","state","city",n);

                                           

                                          and in your function definition, add a fourth parameter to accept that value of n.

                                          • 18. Re: xml problems
                                            PopetMaster Level 1

                                            If I have understood you I need to pass n in the parameters... and if I'm not mistaken, I need to change this line

                                             

                                            var citywasselected:Object = new Object();
                                            citywasselected.change = function(referencia){
                                            if(referencia.target.value != '') show_price(referencia.target.value,combo1.value,combo2.value);
                                            }

                                             

                                            Right now this has  been so

                                             

                                            var citywasselected:Object = new Object();
                                            citywasselected.change = function(referencia){
                                            if(referencia.target.value != '') show_price(referencia.target.value,combo1.value,combo2.value,price.value);
                                            }

                                             


                                            And my function  show_price is so:

                                             

                                            var show_price:Function = function(position_country:Number,position_state:Number,position_city:Number):Void{
                                            var list_price:Array = la_lista.firstChild.childNodes[position_country].childNodes[position_state].childNodes[po sition_city].childNodes;
                                            var myprovider:Array = new Array();
                                            prices.dataProvider = myprovider;
                                            for(n=0;n<list_price.length;n++){
                                                myprovider.addItem({label:list_price[n].attributes['price'],data:n});
                                               }
                                            values3 = prices.getSelectedItem().label;
                                            trace(n);
                                            }

                                             

                                            I have a Dynamic Text whit a variable called values3, This should give me the price.

                                            When I ran this movie and selected the respective (country, state, city), this brought me back undefined and trace(n) still give me back 0.

                                            I suspect this 0 is no the position but rather says no matches were found

                                             

                                            Am I  wrong with changes?

                                            • 19. Re: xml problems
                                              kglad Adobe Community Professional & MVP

                                              i can't determine if you've made a step in the right direction.

                                               

                                              what's supposed to happen in the show_price() function.  are you trying to populate another combobox with prices that are specific to a particular country, state and city?  if yes, that looks good.

                                              • 20. Re: xml problems
                                                PopetMaster Level 1

                                                Thanks!

                                                 

                                                I solve this, in part...

                                                 

                                                I fixed my function like this:

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

                                                var show_price:Function = function(position_country:Number,position_state:Number,position_city:Number):Void{
                                                    var list_price:Array = la_lista.firstChild.childNodes[position_country].childNodes[position_state].childNodes[po sition_city].attributes.price;
                                                    var myprovider:Array = new Array();
                                                    price.dataProvider = myprovider;
                                                       
                                                    trace (list_price);
                                                }

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

                                                 

                                                Also change the price values on the xml, for a easy test:

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

                                                <countries>
                                                    <country label="COUNTRY-ONE">
                                                    <state label="STATE-A">
                                                        <city label="CITY-1A" price="111"/>
                                                                <city label="CITY-2A" price="222"/>
                                                    </state>
                                                    <state label=" STATE-B ">
                                                        <city label="CITY-1B" price ="333"/>
                                                    </state>
                                                    <state label=" STATE-C ">
                                                        <city label="CITY-1C" price="444"/>
                                                    </state>
                                                   </country>

                                                 


                                                   <country label="COUNTRY-TWO">
                                                    <state label=" STATE-1">
                                                        <city label="CITY-A1" price="555"/>
                                                        <city label="CITY-A2" price="666"/>
                                                    </state>
                                                    <state label=" STATE-2">
                                                        <city label="CITY-B1" price="777"/>
                                                        <city label="CITY-B2" price="888"/>
                                                    </state>
                                                   </country>
                                                </countries>

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

                                                 

                                                 

                                                Here my new problem:

                                                When I select COUNTRY-ONE, STATE-A, CITY-1A, this returns  a value 111

                                                to COUNTRY-ONE, STATE-A, CITY-2A, this returns 222

                                                 

                                                But when I select:

                                                COUNTRY-ONE, STATE-B, CITY-1B, this returns  a value 555 (its  value is 333),

                                                to COUNTRY-TWO, STATE-1,CITY-A1, this returns  a value 333 (its  value is 555) and

                                                to COUNTRY-ONE, STATE-C ,CITY-1C this returns undefined (its  value is 444)

                                                 

                                                Why its crossing  and losing those values? I do not understand  about this?