11 Replies Latest reply on Dec 7, 2008 9:18 AM by kglad

    XML Headache

      Hi All

      I am trying to store values in an Array called titles. While inside the onLoad function I can store attributes from the XML tree to the Array 'titles'. My problem is, when I access the titles Array from outside the onLoad function the array is empty. The ActionScript and and the stripped down XML is below, if anybody could please help!



      var jbx:XML = new XML();
      jbx.ignoreWhite = true;

      var termArray:Array = new Array();
      var titles:Array = new Array();
      var p:Number = new Number();
      var names:Array = new Array();

      // Listener that retrieves data from XML file
      jbx.onLoad = function(success)
      if (success)
      termArray = jbx.firstChild.childNodes;
      for (p=0;p<termArray.length;p++)
      title_txt.text = "OOP's not working! Try again later.";

      // XML
      <?xml version="1.0" encoding="ISO-8859-1"?>
      <term name="amplitude" ref="" content="The amplitude is the magnitude of sound pressure change within a waveform(volume), or basically, the maximum amount of pressure at any point in a sound wave. Amplitude is often referred to as sound pressure level and measured in decibels."/>
      <term name="automation" ref="" content="Automation is when the methods of editing are pre-programmed by a user. An example would be changing the volume or pan settings or certain devices are activated according to when a user wishes to deploy them."/>
      <term name="attack" ref="" content="Attack is the moment the sample starts, changing the attack settings will determine where in the sample a device will start to play the sample."/>
        • 1. Re: XML Headache
          kglad Adobe Community Professional & MVP
          when you prefix a variable with "var" inside a function, you make that variable local to the function. ie, it doesn't exist outside the function and doesn't even exist if you exit and re-enter the function.
          • 2. Re: XML Headache
            JohnWalsh2011 Level 1
            Hi KGLAD

            I understand programming fundamentals, I am aware of what goes on inside a function stays inside a function. I study in Java and PHP a lot.

            Do you have any ideas about how to solve my problem. I am only creating this xml object for fun, but I don't like getting a headache when I'm enjoying myself!

            Maybe I should invoke a custom class from the onLoad function and pass the Array to its constructor. Is this a good idea???


            • 3. Re: XML Headache
              kglad Adobe Community Professional & MVP
              actually, i see titles is not prefixed by "var" inside jbx.onLoad. so, it will be available outside jbx.onLoad anytime after it's populated.

              if trace(titles) reveals what you expect insode jbx.onLoad, and trace(titles) is undefined AFTER the onLoad method executes, you have a path error to titles.
              • 4. Re: XML Headache
                JohnWalsh2011 Level 1
                trace() is working inside the function, outside it is UNDEFINED. If I typecast each value from the XML as a String:

                titles.push( String(termArray[p].attributes.name) );

                Would this solve the Path problem?

                • 5. Re: XML Headache
                  kglad Adobe Community Professional & MVP
                  no, that wouldn't solve any path problems.

                  from what timeline (relative to the one that contains your posted code) are you trying to reference titles? and when (relative to the onLoad event) are you trying to reference titles?
                  • 6. Re: XML Headache
                    JohnWalsh2011 Level 1
                    I guess it is the root timeline. All that code is placed in the first frame of the only layer in the Flash timeline. Would the XML.onLoad() function be in a different timeline?. I'm confused by this.

                    Anyway I used the XML.onLoad() function as a constructor and populated a List component and a TextArea component. It works fine that way, but I would love to know why I cannot use the values retrieved from the XML object outside of the XML.onLoad() function? Do you know?

                    Link here to a working version & CODE ATTACHED

                    • 7. Re: XML Headache
                      kglad Adobe Community Professional & MVP
                      when (relative to the onLoad event) are you trying to reference titles?
                      • 8. Re: XML Headache
                        JohnWalsh2011 Level 1
                        Ah ha I think I see what you mean now or I'm more confused.

                        When I populate 'titles' inside the onLoad() function, it is only within that function that the XML has loaded, everywhere else in the code is instantiated prior to the XML being loaded!

                        Is this the Reason?
                        • 9. Re: XML Headache
                          kglad Adobe Community Professional & MVP
                          i don't know. i've been mentioning that you can't reference titles until AFTER the onLoad method executes. and i've been asking when you're trying to reference titles.

                          the onLoad() is executed asynchronously. code that follows the onLoad() doesn't necessarily execute after the onLoad() method.
                          • 10. Re: XML Headache
                            JohnWalsh2011 Level 1
                            Thanks for that, I could not get my head around how the onLoad() function worked, I understand now.

                            The reason I want to reference the values outside the onLoad() function is, I am trying to create a Relational 2D Array that allows me to sort the Data using a primary key.

                            Its just for fun, and the solution I used above lets me use the properties of a selected List Item as a primary key to access the other Arrays/Entities.

                            good luck and thanks again kglad!

                            • 11. Re: XML Headache
                              kglad Adobe Community Professional & MVP
                              you're welcome.

                              in the onLoad() method, after parsing the xml file, i call a function that utilizes the parsed data and starts the application part that depends on the xml data.