4 Replies Latest reply on Oct 25, 2007 10:24 AM by msabljic

    Problem with loading multiple external xml files

    msabljic Level 1
      I have an array named 'source' which consists of external xml file names whose content i am trying to load into another array called 'content'. I tried with following:

      ----------------------------------------------------------------------------------------
      for (i=0; i<source.length; i++){
      XML_URL = " http://localhost/temp/" + source ;
      myXMLURL = new URLRequest (XML_URL);
      myLoader = new URLLoader (myXMLURL);
      myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
      }

      function xmlLoaded(evt:Event):void{
      content.push(myLoader.data);
      }
      ---------------------------------------------------------------------------------------

      The problem is that this way i only get content of the last xml file and other 'content' elements are 'undefined'. Does anyone know solution to this problem or maybe have another way of doing this?

      thanks in advance
        • 1. Problem with loading multiple external xml files
          DCalisuni
          Alright,
          I have this hammered out for you.

          By the time the Event.COMPLETE is activated, your variable myLoader has already changed! Therefore you cannot use it in the function xmlLoaded. This is also not the correct way of using event handlers. You should be using:
          content.push(evt.target.data);

          This way, it is specific to the instance of the loader that has just completed.

          Also, put the addEventListener before you initialize the download always.

          If this answers your problem, please mark as solved.
          • 2. Problem with loading multiple external xml files
            msabljic Level 1
            I tried with your advice and changed those two things (position of addEventListener and used content.push(evt.target.data) ) but i still get only one xml file in my 'content' array so i am confused. Do you have any other suggestions?

            thanks for your help
            • 3. Problem with loading multiple external xml files
              DCalisuni Level 1
              I have tested the following with two xml files in the same folder (I would try the same) and put their names in the array, source. Create two dummy xml files for testing.
              Here is my exact code (I changed i to a because it makes it italicized here in the forums):

              var content:Array = new Array();
              var source:Array = new Array("media1.xml","media2.xml");
              for (var a in source){
              var XML_URL = "" + source[a];
              var myXMLURL = new URLRequest (XML_URL);
              var myLoader = new URLLoader ();
              myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
              myLoader.load(myXMLURL);
              }

              function xmlLoaded(evt:Event):void{
              content.push(evt.target.data);
              if(content.length ==source.length)
              tr();
              }

              function tr(){
              trace(content);
              }
              • 4. Re: Problem with loading multiple external xml files
                msabljic Level 1
                Thans a lot for your time and help DCalisuni, your solution of course works. The problem was addEventListener position, if i use this code:

                XML_URL = " http://localhost/temp/" + source ;
                myXMLURL = new URLRequest (XML_URL);
                myLoader.addEventListener(Event.COMPLETE, xmlLoaded); <----
                myLoader = new URLLoader (myXMLURL);


                Then i only get one item in my array, but with following code (or with the code in your last example):

                XML_URL = " http://localhost/temp/" + source;
                myXMLURL = new URLRequest (XML_URL);
                myLoader = new URLLoader (myXMLURL);
                myLoader.addEventListener(Event.COMPLETE, xmlLoaded);

                i get them all.

                thank you very much once again, you've been really helpful and i appreciate it.

                cheers