By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,300 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,300 IT Pros & Developers. It's quick & easy.

How to use javascript to pull randomly from an XML file

P: n/a
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

Oct 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
TPK wrote:
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
>
Links to any help would be appreciated. Thank you,

TPK
Oct 3 '06 #2

P: n/a
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

Erwin Moller wrote:
TPK wrote:
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

Links to any help would be appreciated. Thank you,

TPK
Oct 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.