How to use javascript to pull randomly from an XML file

T

TPK

I have an HTML document with Javascript where I have a portion of the
page contents, a series of questions, being pulled from a XML file.
When the page dynamically builds all the questions and selection
choices are listed top down in the order they appear in the XML file.

Here is an example of the XML formatting:

<question type="single_answer">The text of a question.
<answer correct="no">A. Answer One
<user_feedback>Incorrect Answer</user_feedback>
</answer>

<answer correct="yes">B. Answer Two
<user_feedback>Incorrect Answer</user_feedback>
</answer>
</question>

As this stands, the entire contents of the XML file is displayed. If I
have 10 questions in the XML file, I get 10 questions being displayed
on the HTML file (in the same order they appear in the XML file). I
would like to have the HTML/Javascript select a random sub-set (and
order them randomly) of the questions rather than the entire contents.

For example, if I have 45 questions, I might like to have a displayed
sub-set of 20 questions that would change each time the page is loaded.

How could I use Javascript to randomly pull from an XML file?
Can anyone point me to instructions as to how to do that using
Javascript?

Links to any help would be appreciated. Thank you,

TPK
 
E

Erwin Moller

TPK said:
I have an HTML document with Javascript where I have a portion of the
page contents, a series of questions, being pulled from a XML file.
When the page dynamically builds all the questions and selection
choices are listed top down in the order they appear in the XML file.

Here is an example of the XML formatting:

<question type="single_answer">The text of a question.
<answer correct="no">A. Answer One
<user_feedback>Incorrect Answer</user_feedback>
</answer>

<answer correct="yes">B. Answer Two
<user_feedback>Incorrect Answer</user_feedback>
</answer>
</question>

As this stands, the entire contents of the XML file is displayed. If I
have 10 questions in the XML file, I get 10 questions being displayed
on the HTML file (in the same order they appear in the XML file). I
would like to have the HTML/Javascript select a random sub-set (and
order them randomly) of the questions rather than the entire contents.

For example, if I have 45 questions, I might like to have a displayed
sub-set of 20 questions that would change each time the page is loaded.

How could I use Javascript to randomly pull from an XML file?
Can anyone point me to instructions as to how to do that using
Javascript?

Look up:
Math.random()
Math.floor()

For random and rounding logic.

Since you decided to use XML, you can do the xml-parsing yourself. :p
(XML is often more trouble than its worth IMHO)

Regards,
Erwin Moller
 
T

TPK

Thankx Erwin,

After having thought about it, I think you are correct. That approach
seems more trouble than its worth.

Having said that I was thinking why not have several XML documents and
randomize their selection.

Right now I use javascript to load a single XML file into a HTML page.
This is the javascript I use:

var pArray = unescape(location.search.substring(1)).split("&");
pArray = ["xml=quiz_1","tracking=scorm"];

if (pArray.length < 2)
alert("Missing parameters");
else
{
var args=pArray[1].split("=")[1];
g_oAssessment.setPassingScore(passingScore);
g_onCompleteAssessment.bSendScore=true;
initialize(args);
if(DATA_getStatus()=="not attempted")
{
DATA_setStatus("incomplete");
}
var sXMLFile = pArray[0].split("=")[1] + ".xml";
var sTrackingMethod = pArray[1].split("=")[1];
openerWindow = window;
loadXML(sXMLFile);
CreateQuestions(sTrackingMethod);

}

I'd like to have several different XML files (structure is the same,
content is different) in a "pool". The pool would have, quiz_1.xml,
quiz_2.xml, quiz_3.xml, etc.)
I'd like the script to randomly select from the pool one of the xml
files and load it.

I have been fussing with the line:
pArray = ["xml=quiz_1","tracking=scorm"];

Trying to build an array such as:
pArray[0] = ["xml=quiz_1","tracking=scorm"];
pArray[1] = ["xml=quiz_2","tracking=scorm"];
pArray[2] = ["xml=quiz_1","tracking=scorm"];

Something appears to break. I get my error message "Missing
Parameters". I think if I can get the array worked out, I can then
randomize using Math.random and Math.floor.

In your opinion, does that sound like a better approach?

TPK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top