• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

Can't use array created in XML onload outside of XML onload - urgent 500pts

I'm pulling data for a gallery from an XML file:

<?xml version="1.0" encoding="utf-8"?>
<Gallery>
      <category catName="categoryname" catNum="1" catSci="sciname">
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="test1"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="test2"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="test3"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="test4"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="test5"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="test6"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="test7"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="test8"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="test9"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="test10"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="test11"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="test12"> </photo>
      </category>
      <category catName="categoryname" catNum="2" catSci="sciname">
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="2test1"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="2test2"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="2test3"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="2test4"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="2test5"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="2test6"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="2test7"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="2test8"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="2test9"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="2test10"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="2test11"> </photo>
            <photo pageNum="1" imgNum="1" comName="" sciName="" refID="" infoTxt="" imgName="2test12"> </photo>
      </category>
</Gallery>

Here's the AS:

      var loadCatNum = 1;
      var loadPageNum = 1;
      //vars for building each gallery page
      var imgName:Array = new Array();
      var comName:Array = new Array();
      var sciName:Array = new Array();
      var refID:Array = new Array();
      var infoTxt:Array = new Array();
      var pageNum:Array = new Array();
      
      xmlGallery = new XML();
      xmlGallery.ignoreWhite = true;
      xmlGallery.load("gallery.xml");
      
      
      xmlGallery.onLoad = function(status) {
            if (!status) {
                  trace(status);
            }
            var gals = this.childNodes[0];
            for (var q = 0; q<gals.childNodes.length; q++){
                  var entries =  gals.childNodes[q];
                  if ((entries.nodeName == "category") && (entries.attributes.catNum == loadCatNum)){
                        for (var i = 0; i<entries.childNodes.length; i++) {
                              var entry = entries.childNodes[i];
                              if ((entry.nodeName == "photo") && (entry.attributes.pageNum == loadPageNum)){
                                    trace("test");
                                    imgName.push(entry.attributes.imgName);
                                    trace(imgName[i]);
                                    comName[i] = entry.attributes.comName;
                                    trace(comName[i]);
                                    sciName[i] = entry.attributes.sciName;
                                    trace(sciName[i]);
                                    refID[i] = entry.attributes.refID;
                                    trace(refID[i]);
                                    infoTxt[i] = entry.attributes.infoTxt;
                                    trace(infoTxt[i]);
                              }//end photo/pagenum if                                                
                        }//end for
                  }//end categoty/catnum if
            }//end for
      }//end onload
      trace(imgName[0]);


Now, if I put that last trace anywhere inside the } after onload, I get a result, outside, I get 'undefined'.  These arrays are to be used in several places, I can't bring them all inside the loops...  HELP!!!
0
AcydTrip
Asked:
AcydTrip
  • 2
  • 2
1 Solution
 
Aneesh ChopraCommented:
XML loading is asyncronous action, you can use the XML data (or arrays created using XML data) only after XML gets loaded, means only after "onLoad" gets executed.

Hence, you should create a function which will hold the code which you want should use arrays, and call this function inside "onLoad" function,
which mean, "onLoad" will load the data, populate the array and call this function to execute the code further...

-------------------
Aneesh Chopra
-------------------
0
 
AcydTripAuthor Commented:
That makes perfect sense now, problem is, when I put the rest of the code in a function, or place it just before the closing } of the onload, it fails to execute.  Is there any tip or trick to time this right, best practice or something?
0
 
Aneesh ChopraCommented:
it would the issue of function scope, I can comment on it, if you provide me the code with function call also..
also tell me where have you written the function code, i mean, is the function on same frame /class or  not

0
 
AcydTripAuthor Commented:
Thanks, I'm going to give it a shot with the following; call the XML with an on release, then at the end, rather than calling a function within the onload, I'll load the clip that contains the code I want to feed the array to.

If it doesn't work, I'll post another Q, I don't want to scope creep this one on you though ;)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now