18 Replies Latest reply on Apr 13, 2007 11:04 AM by Newsgroup_User

    Global Array from XML Nodes

    bigj9901
      I am trying to create an Array by looping through the nodes in an XML doc. I can successfully create the array but it is only accessible within the loadXML function it was created in. How can I make the array accessible to all functions within my flash movie not just inside the function? Heres my AS

      //load XML function
      function loadXML(loaded) {
      if (loaded) {
      //create array to hold w content
      wcontent=new Array();
      //create varibales for assignment loop
      amount=this.firstChild.childNodes.length;
      i=0;
      //assignment loop
      for (var i=0; i<amount; i++) {
      // get node values
      _root.wheading = this.firstChild.childNodes .childNodes[0].firstChild.nodeValue;
      _root.wcontent = this.firstChild.childNodes
      .childNodes[1].firstChild.nodeValue;
      //create vars
      var thewcontent;
      thewcontent = "<span class='heading'>"+_root.wheading+"</span>"+_root.wcontent;
      thewcontent=searchAndReplace(thewcontent, "</p>", "</p><br>")
      // content array
      wcontent =thewcontent;
      }
      // assign vars to text fields
      welcome.styleSheet = myCSS;
      welcome.htmlText = wcontent[w];
      }
      }
        • 1. Re: Global Array from XML Nodes
          Level 7
          bigj9901,

          > How can I make the array accessible to all functions
          > within my flash movie not just inside the function?

          This is a classic scope issue. Variables declared inside functions are
          only available to those functions. So for example, here ...

          > function loadXML(loaded) {
          > if (loaded) {
          > //create array to hold w content
          > wcontent=new Array();

          ... your wcontent array will die as soon as this function ends. To make
          your variable accessible outside the function, declare it outside the
          function ...

          var wcontent:Array = new Array();
          function loadXML(loaded:Boolean):Void {
          if (loaded) {
          // etc.


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Global Array from XML Nodes
            Greg Dove Level 4
            Its not easy for me to understand what you're doing here...but in general for it to be accessible to the other functions just make sure you define it outside your function. You could then initialise it to wcontent=[] at the beginning of this function.
            However it appears that you're not pushing values onto it in this function and just assigning it an html string.


            Otherwise I can't see how you would end up with an array - but like I said, I don't fully understand what it is you're trying to do, so sorry if I'm way off. [The italics tags made your i loop array iterators disappear.]



            • 3. Re: Global Array from XML Nodes
              Greg Dove Level 4
              Also why are you using _root.wcontent as a string? That's probably the reason other functions can't see your array. Give your array a different name so there's no conflict of names
              • 4. Re: Global Array from XML Nodes
                kglad Adobe Community Professional & MVP
                he's not using duplicate names. that's an issue caused by this forum ( [ i ] is interpreted as encoding for italic font):

                when displaying code, use the attach code option or else you risk your code being rendered incorrectly by this forum.
                • 5. Re: Global Array from XML Nodes
                  Greg Dove Level 4
                  kglad: thanks, I picked that up part of that but hadn't picked up all the missing pieces. So I presume the last assignment has an array index before
                  =thewcontent
                  In which case the change to pushing onto the array is not necessary. But I'm still confused with the use of _root.wcontent and wcontent and the reason for doing it ...it seems that this could be related to the cause of the problem... or maybe the xml hasn't loaded and been processed yet before the other functions are trying to use the array.

                  And I just saw David Stiller's comment. I understand that but I don't think that applies here because the array wasn't declared as a local variable (with var) inside the function.

                  • 6. Re: Global Array from XML Nodes
                    Level 7
                    GWD,

                    > And I just saw David Stiller's comment. I understand that but
                    > I don't think that applies here because the array wasn't declared
                    > as a local variable (with var) inside the function.

                    Even without that keyword, the variable is local to the function. My
                    personal preference is to use var, because that helps me see at a glance
                    when I'm choosing to create a variable (as opposed to simply updating one).


                    David Stiller
                    Adobe Community Expert
                    Dev blog, http://www.quip.net/blog/
                    "Luck is the residue of good design."


                    • 7. Global Array from XML Nodes
                      Greg Dove Level 4
                      David:
                      try this, you'll see what I mean:
                      hello = [23,23,23]

                      function test() {
                      hello = new Array();
                      hello = [46,46,46]
                      }

                      trace(hello); //outputs 23,23,23
                      test();
                      trace(hello); //outputs 46,46,46

                      • 8. Re: Global Array from XML Nodes
                        Greg Dove Level 4
                        So bigj9901 perhaps its something to do with either using the same variable name for 2 different purposes... or the xml not having been read and loaded (and therefore the array not populated) before your other functions are attempting to use it.
                        • 9. Re: Global Array from XML Nodes
                          Level 7
                          GWD,

                          > David:
                          > try this, you'll see what I mean:

                          Yup, I see what you mean. I goofed on that one. :)

                          Another way of putting it is like this ...

                          arrayOne = [23,23,23]

                          function test() {
                          trace("test() referencing arrayOne: " + arrayOne);
                          arrayTwo = [46,46,46];
                          }

                          trace("main timeline referencing arrayOne: " + arrayOne)
                          test();
                          trace("main timeline referencing arrayTwo: " + arrayTwo);

                          Based on what I had said, the final trace() statement should show
                          "undefined," but it doesn't. Nothing like cold, hard reality to keep my on
                          my toes. ;)

                          Even so, I encourage the use of the var keyword. Use it to purposefully
                          scope variables where you want them. I'm at a loss to correspond your
                          sample code (definitely useful) against what works for me. I wish I could!
                          In my daily work, it's just standard practice to declare an array outside
                          the XML.onLoad function, if I'm going to populate that array from the XML.


                          David Stiller
                          Adobe Community Expert
                          Dev blog, http://www.quip.net/blog/
                          "Luck is the residue of good design."


                          • 10. Re: Global Array from XML Nodes
                            Greg Dove Level 4
                            David: I agree with you, and I tend to do the same as you... if the array has not been declared outside the function, then its 'declared' here for the first time with the assignment to an empty array.

                            I can't think of more than what I put in my previous post as the potential causes of bigj9901 's issue. I suspect it may be that the other functions seeking to access it before the XML has loaded and this function has run.
                            • 11. Re: Global Array from XML Nodes
                              bigj9901 Level 1
                              Thanks for the feed back. I moved the array declaration outside of the function. and changed the name of the array. Now its producing the desired results.

                              Thanks again!
                              • 12. Re: Global Array from XML Nodes
                                Level 7
                                GWD,

                                > causes of bigj9901 's issue. I suspect it may be that the
                                > other functions seeking to access it before the XML has
                                > loaded and this function has run.

                                Any reason you couldn't access the XML object itself, rather than create
                                the array? XML navigation is tedious, I realize, but it the data are
                                already there....


                                David Stiller
                                Adobe Community Expert
                                Dev blog, http://www.quip.net/blog/
                                "Luck is the residue of good design."


                                • 13. Re: Global Array from XML Nodes
                                  Level 7
                                  > Any reason you couldn't access the XML object itself, rather
                                  > than create the array? XML navigation is tedious, I realize,
                                  > but it the data are already there....

                                  Ha! Woops, I lost track of who posted what. I was thinking this was
                                  your post all along, GWD. Time for me to nap for a while. ;)


                                  David Stiller
                                  Adobe Community Expert
                                  Dev blog, http://www.quip.net/blog/
                                  "Luck is the residue of good design."


                                  • 14. Re: Global Array from XML Nodes
                                    Greg Dove Level 4
                                    "Ha! Woops, I lost track of who posted what. I was thinking this was
                                    your post all along, GWD. Time for me to nap for a while. ;)"

                                    David: No probs... I had a suspicion you did.. lol. For what its worth I agree with you about XML navigation, although I've kinda got my head around the as2 XML object now. I haven't started using as3 yet (soon, soon!) but it seems that will be easier...
                                    • 15. Re: Global Array from XML Nodes
                                      Level 7
                                      GWD,

                                      > I've kinda got my head around the as2 XML object now.
                                      > I haven't started using as3 yet (soon, soon!) but it seems
                                      > that will be easier...

                                      Yes. I haven't used the E4X format yet myself (only read about it), but
                                      it seems sweet. Have you toyed with the Xpath API in AS2 at all? This is
                                      an old URL, but it still works (opens a PDF) ...

                                      http://www.macromedia.com/go/xpathapi


                                      David Stiller
                                      Adobe Community Expert
                                      Dev blog, http://www.quip.net/blog/
                                      "Luck is the residue of good design."


                                      • 16. Re: Global Array from XML Nodes
                                        Greg Dove Level 4
                                        David:

                                        Thanks, and no - I have looked at it a few times, (and I'm finally at the point where I understand it) but haven't used it yet. I've used some 3rd party XML prototype extensions for things like getElementListByName (author Mike Brittain) etc, but not XPath yet. Maybe next time.
                                        • 17. Re: Global Array from XML Nodes
                                          bigj9901 Level 1
                                          I have haven't really done much with XML and flash. I will look into the XML object and how to navigate it.

                                          Thanks
                                          • 18. Re: Global Array from XML Nodes
                                            Level 7
                                            bigj9901,

                                            > I have haven't really done much with XML and flash. I will
                                            > look into the XML object and how to navigate it.

                                            You'll be navigating it to feed that array anyway, right? ;)

                                            It can be useful to "de-complexify" the XML data into a less cluttered
                                            format like an array, so that may be the way you want to go ... on the other
                                            hand, there really isn't a need to duplicate the data. Ultimately, do what
                                            falls most easily into your programming comfort level -- but I like to at
                                            least raise the alternatives.


                                            David Stiller
                                            Adobe Community Expert
                                            Dev blog, http://www.quip.net/blog/
                                            "Luck is the residue of good design."