6 Replies Latest reply on Aug 9, 2007 2:26 PM by clbeech

    Array from an XML integration

    Nejkodebejko1
      Hi,

      i have a problem. I would like to retrive xml data from an external file. An than i would collect data from that xml to be compared with some id buttons.

      ex. IF (xml output atributte) == (id of button), than dissplay button red, else dissplay blue.

      I have no problems till here. This is the array and its conntent.

      var dataValues = new Array();
      dataValues[0] = "100";
      dataValues[1] = "131";

      if i do it manualy, like you see above, it all works fine. The buttons respond to it just fine and have the correct color. But if i try to implement it to a load from the xml file all the buttons stay blue.

      I dont know how to set an array in this:


      function loadXML(loaded) {

      if (loaded) {
      dataValues[42] = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;

      // EVEN IF I TRY dataValues[1] = "200" or something. It just doesent work in that function
      }

      }
      xmlData = new XML();
      xmlData.ignoreWhite = true;
      xmlData.onLoad = loadXML;
      xmlData.load("buttons.xml");

      Please help. Ive been trying all day and no success.
        • 1. Re: Array from an XML integration
          Greg Dove Level 4
          loadXML is probably running after you have already tried to set your button colours, because it only runs when your XML has been loaded. Other instructions continue to run before then.

          so try this:

          At the end of your function loadXML(loaded) call the code that sets the button colours from the dataValues array.

          • 2. Array from an XML integration
            clbeech Level 3
            It looks like you might be missing a few things here. the function loadXML will not execute because the call in the 'XMLdata.onLoad=loadXML;' method is incorrect, and there is no other call to initiate the function. I'm not sure why you're assigning the value from the xml to dataValues[42], but make sure that your pulling from that index for your button color. If your trying to retrieve an array of values from the xml, you may need to iterate through the xml with a 'for' loop, and assign them to each index.

            Try it this way:
            • 3. Array from an XML integration
              Greg Dove Level 4
              @clbeech: in terms of 'XMLdata.onLoad=loadXML;' .... I'm pretty sure that's not an issue. loadXML is a defined function - and it correctly checks the 'loaded' argument that it would receive, so it can be assigned as the onLoad handler.
              You don't have to do a direct assignment for the onLoad handler. I tend to do things in a way similar to your example, but the other way is also fine and there are some times when its useful. The thing I always get left uncertain about (and need to check each time I used it) is the scope in which the function executes when things are done with the separately defined function. I think its still the XML object, but I can never remember and always need to check/test it. (If it wasn't then the code as it stands wouldn't work, but I think its OK).

              I don't understand the [42] sole reference either, but I assumed it was for testing, either way whatever values are being used from the xml are only available as the onLoad handler's code executes.
              • 4. Re: Array from an XML integration
                clbeech Level 3
                GWD, that's interesting, I haven't ever really thought about it in those terms as a 'predefined' substitute function, cool. I've seen other scripts written that way before, but never had any success when trying them, but I don't think I'd put them together properly(without this in mind).

                However, doesn't there still need to be a set of '()' on the function call/assignment here? Could that be why it's not executing?

                I've just learned to use strict syntax, it seems to cut down on errors ... some of the time ;) (I understand that AS3 forces this? haven't seen it yet )
                • 5. Array from an XML integration
                  Greg Dove Level 4
                  @clbeech:

                  However, doesn't there still need to be a set of '()' on the function call/assignment here? Could that be why it's not executing?

                  > No in this case you don't put the () otherwise the function executes when the assignment is made and it would be trying to assign whatever return value the function had to the 'onLoad'. When you assign it to function(success:Boolean) - its being assigned to an anonymous function. I believe its being executed, its just that whatever code that assigns colours to the buttons is trying to do it before (somewhere else) and dataValues[42] doesn't have the right (any?) value yet.

                  I've just learned to use strict syntax, it seems to cut down on errors ... some of the time ;) (I understand that AS3 forces this? haven't seen it yet )

                  >There are no syntax issues with this. It's one of the many event handling options in as2. In as3 its down to one event handling model ... I think. I've only dabbled with as3 and I kinda like what I see.
                  • 6. Re: Array from an XML integration
                    clbeech Level 3
                    Great, GWD, I understand now. It always amazes me, how many different methods there are available to Flash and OOP in general, and how many ways there are to accomplish any given task. I learn something new every day!