Skip to main content
Participant
May 31, 2011
Answered

Using global array for XML results

  • May 31, 2011
  • 2 replies
  • 660 views

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=full_niz.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

This topic has been closed for replies.
Correct answer kglad

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=full_niz.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 replies

Ned Murphy
Legend
May 31, 2011

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=full_niz.childNodes[0].firstChild;

to the following helps...

_global.autoid=full_niz.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.

Participant
May 31, 2011

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=full_niz.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!

kglad
Community Expert
Community Expert
May 31, 2011

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=full_niz.childNodes[0].firstChild;

          }
    traceF();
  }//end function(loaded)
 
pokXML.load("my.xml");
}//end ParsingXML function
 
 
ParsingXML();

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


kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
May 31, 2011

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=full_niz.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]