5 Replies Latest reply on Jun 18, 2009 3:22 PM by kglad

    Finding records in an xml file

    wotnohair

      I am writing a Flash object which will be an on-line quiz. I got most of it working ok, but it reads the questions sequentially from an xml file. I want it to be random. Each record in the xml file has an 'ID' attribute. This is my code that currently reads the questions in sequential order:

       

      var question = new XML();
      question.ignoreWhite=true;
      question.load("Questions.xml");

       

      question.onLoad=function(){
           question_txt=this.firstChild.childNodes[_root.record].attributes.question;
           answer1_txt=this.firstChild.childNodes[_root.record].attributes.answer1;
           answer2_txt=this.firstChild.childNodes[_root.record].attributes.answer2;
           answer3_txt=this.firstChild.childNodes[_root.record].attributes.answer3;
           answer4_txt=this.firstChild.childNodes[_root.record].attributes.answer4;
           right=this.firstChild.childNodes[_root.record].attributes.correct;
      }

       

      Each time a new question is required, this statement is called:

       

      question.load("Questions.xml");

      There are 300 questions in the xml file. I want to assign a variable with a random number, then look in the xml file for the question which has the ID attribute matching the random number. How do I achieve this?

       

      Ian

        • 1. Re: Finding records in an xml file
          kglad Adobe Community Professional & MVP

          read your xml file once, store the records in an array and then access whichever array element you want.  (and even if you wanted to continue using sequential xml nodes, you should do the same):

           


          var question = new XML();
          question.ignoreWhite=true;
          question.load("Questions.xml");

          var qnaA:Array=[];

           

          question.onLoad=function(){

          for(var i=0;i<this.firstChild.childNodes.length;i++){

          var obj:Object=new Object();

              obj.question_txt=this.firstChild.childNodes[i].attributes.question;
               obj.answer1_txt=this.firstChild.childNodes[i].attributes.answer1;
               obj.answer2_txt=this.firstChild.childNodes[i].attributes.answer2;
               obj.answer3_txt=this.firstChild.childNodes[i].attributes.answer3;
               obj.answer4_txt=this.firstChild.childNodes[i].attributes.answer4;
               obj.right=this.firstChild.childNodes[i].attributes.correct;

          qnaA.push(obj);

          }
          }

           

           

          if you want to select random questions without repeats, check the array shuffle function that i've posted in these forums numerous times.


          • 2. Re: Finding records in an xml file
            wotnohair Level 1

            Ok thanks for that. I've got my array populated now. How do I access the values contained in say the 50th element of the array? Sorry for the simple question.This is my first Flash project.

            • 3. Re: Finding records in an xml file
              kglad Adobe Community Professional & MVP

              to access the data in the 51st array element:

               

              qnaA[50].question_txt;
              qnaA[50].answer1_txt;
              qnaA[50].answer2_txt;
              etc


              • 4. Re: Finding records in an xml file
                wotnohair Level 1

                Yes that works thank you for your help.

                 

                Ian

                • 5. Re: Finding records in an xml file
                  kglad Adobe Community Professional & MVP

                  you're welcome.