473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to use javascript to pull randomly from an XML file

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
2 2474
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nathan Given | last post by:
Hello All, I am trying to randomly change the background image of my home page but I can't seem to figure it out. Here is a snippet of my css .... BODY {background:transparent...
3
by: robbiehenry | last post by:
1. robbiehe...@gmail.com Sep 19, 1:48 pm show options From: robbiehe...@gmail.com - Find messages by this author Date: Mon, 19 Sep 2005 10:48:50 -0700 Local: Mon, Sep 19 2005 1:48 pm...
3
by: brian | last post by:
Hello, Can someone tell me how I can randomly assign a file to a variable from a directory. Example: Dim File as string I need to search through 'C:/Comics/' and have the program randomly...
6
by: sales | last post by:
Hello, I am trying to get my website checkout page to rotate / take turns displaying shopping comparison engine surveys rather than display them all 4 at the same time, thus overwhelming &...
7
by: trey.bason | last post by:
I know everyone who uses javascript at some point tries to think of a way to hide it from curious users/hackers, so here goes my question. I am trying to display an image map and keep the coords...
2
Chittaranjan
by: Chittaranjan | last post by:
Hi All, I am having a problem using Javascript in Perl script. In the perl module I have included the Javascript codes and that is workign fine for validation purpose but when I am trying to show...
4
epots9
by: epots9 | last post by:
what i am trying to do is i want my javascript to read the contents of a folder, this folder will only contain files (pictures). Once the it reads in all the file names that are there i would store...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.