1 Reply Latest reply on Mar 26, 2010 10:13 PM by kglad

    Importing XML stored variables into as3?

    celebritymusic Level 1

      Hey  guys

      Basically,  my current project has a whole lot of global variables eg



      What  I'd like to do is set each variable in my XML file, and then pass the  value to the global var in my project

      so in the XML file, xmlvar1  might have a value of 8.

      Then in my project, I want:

      globals.data.var1  = the value for xmlvar1 (in other words, 8)

      so a) is there a  simpler way to do this than the way I've adapted Adama's code, and b),  if not, why is the code not working (I'll paste the code below). 

      Basically,  I can get the variable from XML into my as3 code, but after all the  functions for importing the XML, when I tell it to  trace(globals.data.eTP); - it returns undefined.  However, if I tell it  to t
      race(globals.data.eTP); immediately after  the line of code that imports it, it returns 8.  So it's like the rest of my as3 code is trying to deal with  the variable before it's been grabbed from my XML file:

      var myXML:XML = new XML();
      var XML_URL:String =  "edit.xml";
      var myXMLURL:URLRequest = new URLRequest(XML_URL);
      var  myLoader:URLLoader = new URLLoader(myXMLURL);
      myLoader.addEventListener("complete",  xmlLoaded);

      // Create the xmlLoaded function
      function  xmlLoaded(event:Event):void {
          // Place the xml data into  the myXML object
            myXML = XML(myLoader.data);
          //  Initialize and give var name to the new external XMLDocument
             var xmlDoc:XMLDocument = new XMLDocument();
          // Ignore spacing  around nodes
            xmlDoc.ignoreWhite = true;
          // Define a new  name for the loaded XML that is the data in myLoader
            var  menuXML:XML = XML(myLoader.data);
          // Parse the XML data into a  readable format

                // Run the "for each" loop to iterate through all of the menu  items listed in the external XML file
                 for each (var T:XML  in myXML..T) {
                        // Access the value of the "T" node in our external XML  file
                         globals.data.eTP = T.placeMe.toString();
                                      } //end of for each  loop

              }  // end of xmlLoaded function


      Any  ideas what is wrong?  Also, is there a different syntax in the
      globals.data.eTP = T.placeMe.toString(); code for bringing in numbers as opposed to text?

      Thanks  for your help guys.


        • 1. Re: Importing XML stored variables into as3?
          kglad Adobe Community Professional & MVP

          i assume globals is a class that has a static object, data and you've imported globals into the scope that contains your code.


          if that's not true, you have more problems than the two below:


          1.  your for-loop is repeatedly assigning values to the same object property, globals.data.eTP


          2.  you're trace() function executes before your xmlLoaded() function, as mentioned in one of your other threads.