7 Replies Latest reply on Apr 20, 2009 11:34 PM by strugler

    loading xml into separate arrays

    strugler

      The first two records of my XML file are below these are two of about 100.

       


      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <coursesSummary>
      <courses>
      <year>2009</year>
      <quarter>1</quarter>
      <region>Central Courses</region>
      <coursecode>3634500</coursecode>
      <coursetitle>Bancassurance(BIP) 2- Selling Skills Course</coursetitle>
      <coursesrun>1</coursesrun>
      <maxplaces>10</maxplaces>
      <numberattended>9</numberattended>
      <splitbyregion>no</splitbyregion>
      <centralreport>yes</centralreport>
      </courses>
      <courses>
      <year>2009</year>
      <quarter>1</quarter>
      <region>Central Courses</region>
      <coursecode>3634600</coursecode>
      <coursetitle>Bancassurance (BIP) 3-Developing your Business</coursetitle>
      <coursesrun>1</coursesrun>
      <maxplaces>15</maxplaces>
      <numberattended>8</numberattended>
      <splitbyregion>no</splitbyregion>
      <centralreport>yes</centralreport>
      </courses>
      </coursesSummary>

       


      <------------------------------------------------------------>

       

      I need to be able to make an associative array of the elements within the <courses> tag.
      I need to be able to show mix and match the data from the 9 arrays.

       

      I can get the data into a text box but am unable to make the arrays work. In test
      I only get one item of data from the first <courses> tag. the array below just displays 2009

       

      <------------------ .fla below -------------------------------->

       

      var myXML = new XML();
      myXML.ignoreWhite = true;
      myXML.onLoad = loadCourses;
      var intervalID:Number = setInterval(checkLoad, 5, myXML);
      myXML.load("Book2data.xml");
      function loadCourses(success:Boolean):Void {
      if (success) {
      //trace("data");
      } else {
      //trace("no data");
      }
      }
      function loadCourses():Void {
      cNodes = this.childNodes[0].childNodes[0];
      var yearArray:Array = cNodes.childNodes[0].firstChild.nodeValue;
      for (i=0; i<yearArray.length; i++) {
      yearArray[i] = cNodes.childNodes[0].firstChild.nodeValue;
      }
      trace(yearArray);
      courseText.text = cNodes.childNodes[0].firstChild.nodeValue;//year
      /*courseText.text = cNodes.childNodes[1].firstChild.nodeValue;//quarter
      courseText.text = cNodes.childNodes[2].firstChild.nodeValue;//region
      courseText.text = cNodes.childNodes[3].firstChild.nodeValue;//coursecode
      courseText.text = cNodes.childNodes[4].firstChild.nodeValue;//coursetitle
      courseText.text = cNodes.childNodes[5].firstChild.nodeValue;//coursesrun
      courseText.text = cNodes.childNodes[6].firstChild.nodeValue;//maximunplaces
      courseText.text = cNodes.childNodes[7].firstChild.nodeValue;//numberattended
      courseText.text = cNodes.childNodes[8].firstChild.nodeValue;//splitbyregion
      courseText.text = cNodes.childNodes[9].firstChild.nodeValue;//centralreport
      */
      }

        • 1. Re: loading xml into separate arrays
          RossRitchey Level 4

          The reason you are only getting the first node is because you are only setting the first node ever:

           

          for (i=0; i<yearArray.length; i++) {
               yearArray[i] = cNodes.childNodes[0].firstChild.nodeValue;
          }

           

          cNodes.childNodes[0].firstChild.nodeValue will give you the first node, but you are repeating this as many times as yearArray is long

           

          change to:

           

          for (i=0; i<yearArray.length; i++) {
               yearArray[i] = cNodes.childNodes[i].firstChild.nodeValue;
          }

           

          to get the value at each node as you go through the loop.

          • 2. Re: loading xml into separate arrays
            strugler Level 1

            Thanks you so much for the time and trouble you have taken in sending your answer. As soon as i read it I felt very bad that I had made such a silly error. However on amanding the code as you kindly sugested the problem still remains. I only get a trace on of the first valure 2009.

             

            I have tried guggling the code a bit but with no success. Please could you help further by having another look The revised code is below, the xml is in the first post.

             

             

            Thanks again

             

                var cNodes:Array  = this.childNodes[0].childNodes[0];
                var yearArray:Array = cNodes.childNodes[0].firstChild.nodeValue;
               
               
                for (i=0; i<yearArray.length; i++) {
                    yearArray[i] = cNodes.childNodes[i].firstChild.nodeValue;
                }
                trace(yearArray);

            • 3. Re: loading xml into separate arrays
              dmeN Level 5

              May I suggest simply populating a course array with course objects:

               

              var theCourses:XML = new XML();
              theCourses.ignoreWhite = true;
              
              var courseData:Array = new Array();
              
              theCourses.onLoad = function(success) {
                  if(success){
                      var l = theCourses.firstChild.childNodes.length;        
                      for(var i = 0; i < l; i++){
                          var course:Object = new Object();
                          course.year = theCourses.firstChild.childNodes[i].childNodes[0].firstChild.nodeValue;
                          course.quarter = theCourses.firstChild.childNodes[i].childNodes[1].firstChild.nodeValue;
                          course.region = theCourses.firstChild.childNodes[i].childNodes[2].firstChild.nodeValue;
                          course.code = theCourses.firstChild.childNodes[i].childNodes[3].firstChild.nodeValue;
                          course.title = theCourses.firstChild.childNodes[i].childNodes[4].firstChild.nodeValue;
                          //etc.
                          courseData.push(course);
                      }        
                  }    
                  trace(courseData[1].title);
              };
              
              theCourses.load("courses.xml");
              stop();
              
              • 4. Re: loading xml into separate arrays
                strugler Level 1

                Thanks yet again - this looks like a far better idea - but It seems there is a problem with your code :-(

                 

                I get the error message below and have tried to solve it by looking on the web but I was unable get a compile.

                 

                TypeError: Error #1006: value is not a function.
                    at Untitled_fla::MainTimeline/frame1()

                 

                Although I have just started to learn AS3 my knowledge is not good and so far have only worked in AS2. Is it possible for you to translate ths answer to AS2 please?

                 

                Thanks once again for your patience

                • 5. Re: loading xml into separate arrays
                  dmeN Level 5

                  Actually, that is AS2 - this is the AS1 / AS2 forum... and it does work. I copied the XML you posted, and used it as my test - the trace outputs as expected so the error isn't likely in that code. What's the actual code you have now?

                  • 6. Re: loading xml into separate arrays
                    strugler Level 1

                    var courses = new XML();
                    courses.ignoreWhite = true;
                    courses.onLoad = loadCourses;
                    // firse the function loadCourses
                    var intervalID:Number = setInterval(checkLoad, 5, courses);
                    courses.load("Book2data.xml");
                    function loadCourses(success:Boolean):Void
                    {
                        if (success) {
                            //trace("data");
                        } else {
                            //trace("no data");
                        }
                        var cNodes:Array = this.childNodes[0];
                        //ok
                        var courseLength = cNodes.childNodes.length; // = 93
                    var courseData:Array = new Array();

                    theCourses.onLoad = function(success) {
                        if(success){
                            var l = theCourses.firstChild.childNodes.length;
                            for(var i = 0; i < l; i++){
                                var course:Object = new Object();
                                course.year = theCourses.firstChild.childNodes[i].childNodes[0].firstChild.nodeValue;
                                course.quarter = theCourses.firstChild.childNodes[i].childNodes[1].firstChild.nodeValue;
                                course.region = theCourses.firstChild.childNodes[i].childNodes[2].firstChild.nodeValue;
                                course.code = theCourses.firstChild.childNodes[i].childNodes[3].firstChild.nodeValue;
                                course.title = theCourses.firstChild.childNodes[i].childNodes[4].firstChild.nodeValue;
                                //etc.
                                courseData.push(course);
                            }
                        }
                        trace(courseData[1].title);
                    };

                        courseText.text = cNodes.childNodes[i].childNodes[i].firstChild.nodeValue;
                        //year
                        //courseText.text = cNodes.childNodes[0].childNodes[0].firstChild.nodeValue;
                        //year
                        /*    var yearArray:Array = cNodes.childNodes[0].firstChild.nodeValue;
                        // ok
                        courseText.text = cNodes.childNodes[1].firstChild.nodeValue;//quarter
                        courseText.text = cNodes.childNodes[2].firstChild.nodeValue;//region
                        courseText.text = cNodes.childNodes[3].firstChild.nodeValue;//coursecode
                        courseText.text = cNodes.childNodes[4].firstChild.nodeValue;//coursetitle
                        courseText.text = cNodes.childNodes[5].firstChild.nodeValue;//coursesrun
                        courseText.text = cNodes.childNodes[6].firstChild.nodeValue;//maximunplaces
                        courseText.text = cNodes.childNodes[7].firstChild.nodeValue;//numberattended
                        courseText.text = cNodes.childNodes[8].firstChild.nodeValue;//splitbyregion
                        courseText.text = cNodes.childNodes[9].firstChild.nodeValue;//centralreport
                        */
                    }

                     

                    The following errors

                     

                    **Error** Scene=Scene 1, layer=as, frame=1:Line 18: Syntax error.

                     

                    **Error** Scene=Scene 1, layer=as, frame=1:Line 20: Operator '=' must be followed by an operand
                             if(success){

                     

                    **Error** Scene=Scene 1, layer=as, frame=1:Line 21: Syntax error.
                                 var l = theCourses.firstChild.childNodes.length;

                     

                    **Error** Scene=Scene 1, layer=as, frame=1:Line 22: Syntax error.
                                 for(var i = 0; i < l; i++){

                     

                    Total ActionScript Errors: 4      Reported Errors: 4

                     

                    thanks again

                    • 7. Re: loading xml into separate arrays
                      strugler Level 1

                      Thanks so much for you support and the time that you have spent on this.

                       

                      I pasted your code into Flash 8 and still get the following errors. I have

                      tried various amendments to get a compile without success. I know that it

                      compiles for you - what am I doing wrong?

                       

                      The errors are as follows.

                       

                      *Error* Scene=Scene 1, layer=as, frame=1:Line 5: Operator '=' must be

                      followed by an operand

                                   var l = theCourses.firstChild.childNodes.length;

                       

                      *Error* Scene=Scene 1, layer=as, frame=1:Line 7: Syntax error.

                              

                       

                      *Error* Scene=Scene 1, layer=as, frame=1:Line 8: Syntax error.

                                   for(var i = 0; i < l; i++){

                       

                      Total ActionScript Errors: 3       Reported Errors: 3

                       

                      Thanks again