6 Replies Latest reply on May 31, 2011 8:00 AM by kglad

    Using global array for XML results

    Vladimir73

      I'm new in flash scripting and this is driving me crazy...

      I searched the whole internet and I could not find a solution for this.

      I'm trying to load XML, parse values into global array, and use that global array later in my code... this is not working.

       

      _global.autoid=Array();

       

      function ParsingXML(){

        var pokXML:XML = new XML();

        pokXML.ignoreWhite = true;

        pokXML.onLoad = function(loaded){

        if (loaded){

          var full_niz:Array=pokXML.firstChild.childNodes;

          var len:Number=full_niz.length-1//getting the no of child nodes

          for(var i:Number=0;i<=len;i++)//looping trough the values

               {

                _global.autoid[i]=full_niz[i].childNodes[0].firstChild;

                }

           trace(_global.autoid);  //return OK array

          return _global.autoid; //function output

           }else{

                trace("Nije OK");

           }

        }//end function(loaded)

       

      pokXML.load("my.xml");

      }//end ParsingXML function

       

       

      ParsingXML();  //return empty array[/AS]

       

       

      How do I make this work?

      Sorry for my (bad) English

        • 1. Re: Using global array for XML results
          kglad Adobe Community Professional & MVP

          try:

           

          _global.autoid=new Array();

           

          function ParsingXML(){

            var pokXML:XML = new XML();

            pokXML.ignoreWhite = true;

            pokXML.onLoad = function(loaded){

            if (loaded){

              var full_niz:Array=pokXML.firstChild.childNodes;

              var len:Number=full_niz.length-1//getting the no of child nodes

              for(var i:Number=0;i<=len;i++)//looping trough the values

                   {

                    autoid[i]=full_niz[i].childNodes[0].firstChild;

                    }

               trace(_global.autoid)   //return OK array

             // return _global.autoid; // pokXML.onLoad is asynchronous so it doesn't make sense to have a return.

               }else{

                    trace("Nije OK");

               }

            }//end function(loaded)

           

          pokXML.load("my.xml");

          }//end ParsingXML function

           

           

          ParsingXML();  //return empty array[/AS]

           

           


           

          • 2. Re: Using global array for XML results
            Ned Murphy Adobe Community Professional & MVP

            If you are assigning to the array that is declared external to the function, then you do not need to return it at the end of the function.

             

            See if changing the one line...

             

            _global.autoid[i]=full_niz[i].childNodes[0].firstChild;

             

            to the following helps...

             

            _global.autoid[i]=full_niz[i].childNodes[0];

             

             

            I do not know what the structure of the xml file is, so that's just a rough shot at it being a simple xml structure.  You should provide the structure of the xml if that doesn't work for you.  You don't have to provide the actual data, but the general structure of the nodes is important to know in trying to solve this.

            • 3. Re: Using global array for XML results
              Vladimir73 Level 1

              Thank You for answers...

               

              my.xml (very simple):

               

              <?xml version="1.0" encoding="UTF-8"?>
              <dataroot>
                   <calendar>
                        <autoid>1</autoid>
                        <fest>Fest1</fest>
                        <status>2</status>          
                   </calendar>
              
                   <calendar>
                        <autoid>2</autoid>
                        <fest>Fest2</fest>
                        <status>2</status>          
                   </calendar>
              
              </dataroot>
              

               

              I need function to return array of autoid values. My function read values correctly, but I have no output.

               

               

               

              _global.autoid=Array();
               
              function ParsingXML(){
                var pokXML:XML = new XML();
                pokXML.ignoreWhite = true;
                pokXML.onLoad = function(loaded){
                if (loaded){
                  var full_niz:Array=pokXML.firstChild.childNodes;
                  var len:Number=full_niz.length-1//getting the no of child nodes
                  for(var i:Number=0;i<=len;i++)//looping trough the values
                       {
                        _global.autoid[i]=full_niz[i].childNodes[0].firstChild;
              
                        }
                   //(if i trace _global.autoid here, it outputs OK values) 
                }//end function(loaded)
               
              pokXML.load("my.xml");
              }//end ParsingXML function
               
               
              ParsingXML();
              trace (_global.autoid[2]); //this NOT WORKING
              
              
              

               

              For example, later in my code I need first value from my global array. How to get it?

              I need values from that array...

              thanks again!

              • 4. Re: Using global array for XML results
                kglad Adobe Community Professional & MVP

                again, onLoad() is asynchronous:

                 


                 

                 


                 

                 

                 

                _global.autoid=new Array();
                 
                function ParsingXML(){
                  var pokXML:XML = new XML();
                  pokXML.ignoreWhite = true;
                  pokXML.onLoad = function(loaded){
                  if (loaded){
                    var full_niz:Array=pokXML.firstChild.childNodes;
                    var len:Number=full_niz.length-1//getting the no of child nodes
                    for(var i:Number=0;i<=len;i++)//looping trough the values
                         {
                          _global.autoid[i]=full_niz[i].childNodes[0].firstChild;
                
                          }
                    traceF();
                  }//end function(loaded)
                 
                pokXML.load("my.xml");
                }//end ParsingXML function
                 
                 
                ParsingXML();

                function traceF(){
                trace (_global.autoid[2]);
                }

                 


                1 person found this helpful
                • 5. Re: Using global array for XML results
                  Vladimir73 Level 1

                  In other words, there is no way to generate global array and use values later?

                  Is there any other way to do this in AS2?

                  Thanks...

                  • 6. Re: Using global array for XML results
                    kglad Adobe Community Professional & MVP

                    noone said that.

                     

                    you can't use that array until your xml has completed loading and has been parsed.  loading is not instantaneous.  it's asynchronous.

                     

                    ie, any line of code immediately below your ParsingXML() line will execute before loading is complete and before the xml is parsed and before your array is defined.

                     

                    so, use a function call (like traceF) to execute code that depends on your array being defined.

                    1 person found this helpful