Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parse XML problem

Status
Not open for further replies.

dakoz

Programmer
Feb 2, 2002
74
hi all..

i have the following script which parses an XML File and generates a quiz. My problem is the following : In the place where are all the answers and question are in text i want to replace them and put images ... i try giving <IMG SRC=temp.gif> and this didnt work i get errors in my code.

here is the code... any suggestions?

<script>

var objXmlHTTP,objXmlDOM;
var aQuest; //to store question ids
var aAnswer = new Array(); // to track the result
var aSelected = new Array(); // to store user's response
var count = 0; //to store the current question no
var ansSel = 0; //to store user's selection
var timerID; //to store the setInterval fun's id
var radIndex = -1; //to store the selected radio's index
var ExamDuration = 5 * 60 ; // 5 minutes
var showtimer = 1 // variable to decide whether to show the timer or now: 1 - show 0 - hide
var NoQ = 8 // variable to store the number of question to fetch

//constructor like function
//here XML objects are created and
//No of questions as well as question ids list
//are fetched from the server.
function init(){
finishbuttonp.innerHTML = &quot;&quot;
objXmlHTTP = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
objXmlDOM = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
temp = GetQuestionList();
aQuest = temp.split(&quot;,&quot;);

//initialize the user's answers list
for(i=0;i<aQuest.length; i++){
aAnswer = 0; // 0 for wrong; 1 for right answer
aSelected = -1; // to store the radio's index
}

if(count < aQuest.length) {

objXmlDOM.loadXML(GetNextQuestion(aQuest[count]));
//parse the response content fetched from the server
//and display the question
parseQ();
}

//start the timer
if(showtimer == 1)
timerID = setInterval(&quot;timer()&quot;,1000);
}

function getPreQ() {
//update the user's answers list
checkAnswer();

//decrement the question no - i.e. to previous Question
count--;

//stop the timer
if(showtimer == 1)
clearInterval(timerID);

//fetch the question for the aQuest[count] id
objXmlDOM.loadXML(GetNextQuestion(aQuest[count]));

//parse the response content fetched from the server
//and display the question
parseQ();

//start the timer
if(showtimer == 1)
timerID = setInterval(&quot;timer()&quot;,1000);
}

function getNextQ() {
//update the user's answers list
checkAnswer();

//increment the question no - i.e. to next Question
count++;

//stop the timer
if(showtimer == 1)
clearInterval(timerID);

objXmlDOM.loadXML(GetNextQuestion(aQuest[count]));

//parse the response content fetched from the server
//and display the question
parseQ();

//start the timer
if(showtimer == 1)
timerID = setInterval(&quot;timer()&quot;,1000);
}

function parseQ(){
//fetch the question from theXML Object
//format the display
strOut = &quot;<table border=0 align=center width=80%>&quot;;
strOut += &quot;<tr><td colspan=2><b>&quot;;
strOut += &quot;<CENTER>Åñþôçóç : &quot; + (count+1) + &quot; áðï &quot;;
strOut += aQuest.length + &quot;</b></CENTER><HR width=70% style='height: 1pt;color:DFDFDF;'></td></tr>&quot;;
strOut += &quot;<tr><td colspan=2>&nbsp;</td></tr>&quot;;

temp = objXmlDOM.selectSingleNode(&quot;data/qtext&quot;).text;

strOut += &quot;<tr><td colspan=2><b>&quot;+temp+&quot;</b></td></tr>&quot;;
strOut += &quot;<tr><td colspan=2>&nbsp;</td></tr>&quot;;

Nodes = objXmlDOM.selectNodes(&quot;data/choice&quot;);

for(i=0;i<Nodes.length;i++){
strOut += &quot;<tr><td align=center width=10%>&quot;;
strOut += &quot;<input type=radio name=ansUsr &quot;;
strOut += &quot; onClick='ansSel=&quot; + (i+1);
strOut += &quot;;radIndex=&quot; + i + &quot;' &quot;;
strOut += &quot;value=&quot; + (i+1) + &quot;></td><td>&quot;;
strOut += Nodes.item(i).text + &quot;</td></tr>&quot;;
}

//set ansNo (hidden field) to the actual answer
temp = objXmlDOM.selectSingleNode(&quot;data/answer&quot;).text;
document.frm.ansNo.value = temp;

strOut += &quot;<tr><td colspan=2>&nbsp;</td></tr>&quot;;
strOut += &quot;<tr><td colspan=2>&quot;;

/*if(count != 0 ){
strOut += &quot;<input type=button value=Previous &quot;;
strOut += &quot; onClick='getPreQ()'> &quot;;
}*/

if(count < aQuest.length-1)
{
strOut += &quot; <CENTER><input type=button value='Åðüìåíç Åñþôçóç'&quot;;
strOut += &quot; onClick='getNextQ()'></CENTER>&quot;;
}
else
{
strOut += &quot; <CENTER><input type=button value='Ôåëåßùóå ôï ÔÅÓÔ'&quot;;
strOut += &quot; onClick='showResult()'></CENTER>&quot;;
}

strOut += &quot;</td></tr></table>&quot;;

//set the strOut content to <P> tag named QArea
QArea.innerHTML = strOut;

//set the default value to ansSel
ansSel = 0;
radIndex = -1;

//check the radio if user has selected previously
if (aSelected[count] != -1) {
radIndex = aSelected[count];
ansSel = radIndex + 1;
document.frm.ansUsr[radIndex].checked = true;
}
}

function checkAnswer(){
//store the selected radio's index
aSelected[count] = radIndex;

//if the user selection matches the actual answer
if (ansSel == document.frm.ansNo.value)
aAnswer[count] = 1;
else
aAnswer[count] = 0;
}

function showResult() {
rights = 0;

//stop the timer
if(showtimer == 1)
clearInterval(timerID);

//update the user's answers list
checkAnswer();

//count no of answers
for(i=0;i<aAnswer.length;i++){
if(aAnswer == 1)
rights++;
}
strRes = &quot;<h2 align=center><br>&quot;;

//if all the answers are correct then greet
if(rights == aAnswer.length)
strRes += &quot;<br>ÓÕÃ×ÁÑÇÔÇÑÉÁ ÁÐÁÍÔÇÓÁÔÅ ÓÅ ÏËÅÓ ÔÉÓ ÅÑÙÔÇÓÅÉÓ ÓÙÓÔÁ !!<BR>&quot;;

strRes += &quot;<HR width=70% style='height: 1pt;color:DFDFDF;'><br> Ç óùóôÝò óáò áðáíôÞóåéò åßíáé :&quot; + rights + &quot; óôéò &quot; + aAnswer.length
strRes += &quot;<br> Ôï ðïóïóôü åðéôõ÷ßáò óáò åßíáé : &quot; + (rights / aAnswer.length) * 100 + &quot; % &nbsp;<IMG SRC='./images/chart.gif' align=center></h2><HR width=70% style='height: 1pt;color:DFDFDF;'>&quot;
strRes += &quot;<center>Ïé áðáíôÞóåéò ìå ôï <FONT COLOR=GREEN><B><I>ÐÑÁÓÉÍÏ</I></B></FONT> ÷ñþìá åßíáé ïé óùóôÝò êáé ìå <FONT COLOR=RED><B><I>ÊÏÊÊÉÍÏ</I></FONT> áõôÝò ðïõ êÜíáôå ëÜèïò</center><BR><BR><BR>&quot;
strOut = &quot;<table border=0 align=center width='80%' cellpadding=0 cellspacing=0>&quot;;
for(cnt=0;cnt<aQuest.length; cnt++)
{
objXmlDOM.loadXML(GetNextQuestion(aQuest[cnt]));
strOut += &quot;<tr><td align=right valign=middle colspan=2 style='BORDER-RIGHT: #eeeeee 1px solid; PADDING-RIGHT: 5px; PADDING-TOP: 9px; PADDING-BOTTOM: 9px; BORDER-TOP: #eeeeee 1px solid; PADDING-LEFT: 5px;BORDER-LEFT: #eeeeee 1px solid; WIDTH: 100%;BORDER-BOTTOM: #000000 1px solid;'><b>&quot;;
strOut += &quot;<IMG SRC='images/question.gif' align=middle>&nbsp;<FONT FACE=ARIAL>Åñþôçóç &quot; + (cnt+1) + &quot;</FONT></b></td></tr>&quot;;
// strOut += aQuest.length + &quot;</b></td></tr>&quot;;
// strOut += &quot;<tr><td colspan=2>&nbsp;</td></tr>&quot;;

temp = objXmlDOM.selectSingleNode(&quot;data/qtext&quot;).text;

strOut += &quot;<tr><td colspan=2 style='background=#E1E1DE;BORDER-RIGHT: #eeeeee 1px solid; PADDING-RIGHT: 5px; PADDING-LEFT: 5px;PADDING-TOP: 5px; PADDING-BOTTOM: 5px;BORDER-LEFT: #eeeeee 1px solid; BORDER-BOTTOM: #000000 1px solid;'><b><i>&quot;+temp+&quot;</i></b></td></tr>&quot;;
// strOut += &quot;<tr><td colspan=2>&nbsp;</td></tr>&quot;;

temp = objXmlDOM.selectSingleNode(&quot;data/answer&quot;).text;
ans = temp;

Nodes = objXmlDOM.selectNodes(&quot;data/choice&quot;);

for(i=0;i<Nodes.length;i++)
{

strOut += &quot;<tr><td align=left width='70%' style='BACKGROUND=#F4F4E7;BORDER-RIGHT: #eeeeee 1px solid; PADDING-RIGHT: 5px;PADDING-LEFT: 5px;BORDER-LEFT: #eeeeee 1px solid; WIDTH: 100%;'>&quot;;
if(ans-1 == i)
strOut += &quot;<font color='green'><b>&quot; + Nodes.item(i).text + &quot;</b></font>&quot; + &quot; <IMG SRC='./images/check.gif' align=middle></td></tr>&quot;;
else if(aSelected[cnt] == i && aSelected[cnt] != ans-1)
strOut += &quot;<font color='red'><b><i>&quot; + Nodes.item(i).text + &quot;</i></b><FONT FACE=VERDANA SIZE=1> (ËÁÈÏÓ ÁÐÁÍÔÇÓÇ!)</FONT></FONT>&quot; + &quot;</td></tr>&quot;;
else
strOut += Nodes.item(i).text + &quot;</td></tr>&quot;;
}

strOut += &quot;<tr><td style='BORDER-TOP: #eeeeee 1px solid;'>&nbsp;</td> </tr><tr><td><BR><BR></td> </tr>&quot;
}
strOut += &quot;</table>&quot;
strRes += strOut
// strRes += &quot;<br><br><center><Input Type=Button Value='Take The Exam Again !' OnClick=document.location.href='home.html' id=Button1 name=Button1></center>&quot;;
document.write(strRes);
}

var timeCount = 0;
function timer(){
timeCount++; //increment the time by one second

//to display the time in the status bar,
// uncomment the next line
//window.status = &quot;...&quot; + timeCount + &quot; secs&quot; ;

//to display the time
var timeremaining;
timeremaining = ExamDuration - timeCount;
temp = &quot;Time: &quot; + parseInt(timeremaining/60);
temp += &quot; min : &quot; + timeremaining%60 + &quot; sec &quot;;
TBlock.innerText = temp;



//if the time is up
if (timeCount == ExamDuration) {
alert(&quot;Óõãíþìç Ôåëåßùóå ï ×ñüíïò Óáò&quot;);
showResult();
}
}

function GetQuestionList()
{
var obj, temp, strXML, cnt, retstring, TotalQ;
obj = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
obj.async = false;
obj.load(&quot;OLExam.xml&quot;);
Nodes = obj.selectNodes(&quot;data/question&quot;);
retstring = &quot;&quot;;
cnt = 0;
while(cnt < NoQ && cnt < Nodes.length)
{
temp = Nodes.item(cnt).attributes(0).value
retstring += temp + &quot;,&quot;;
cnt++;
}
retstring = retstring.substring(0,retstring.length-1);
return retstring;
}

function GetNextQuestion(QNo)
{
var obj, temp, strXML;
obj = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
obj.async = false;
obj.load(&quot;OLExam.xml&quot;);
temp = &quot;data/question[@id=&quot; + QNo + &quot;]&quot;;
Node = obj.selectSingleNode(temp);
strXML = &quot;<data>&quot;
strXML += &quot;<qtext>&quot;
strXML += Node.selectSingleNode(&quot;qtext&quot;).text
strXML += &quot;</qtext>&quot;
strXML += &quot;<answer>&quot;
strXML += Node.selectSingleNode(&quot;answer&quot;).text
strXML += &quot;</answer>&quot;
Node = Node.selectNodes(&quot;choices/choice&quot;)
for(i=0;i<=Node.length-1;i++)
{
strXML += &quot;<choice>&quot;
strXML += Node.item(i).text
strXML += &quot;</choice>&quot;
}
strXML += &quot;</data>&quot;
return strXML
}
</script>
</HEAD>
<BODY BGCOLOR=#ffffff background=&quot;images/back.gif&quot; topmargin=160>
<CENTER><FONT FACE=ARIAL><H2><SCRIPT language=javascript>{document.write(testOnTop)}</SCRIPT></H2></FONT></CENTER>
<BR><BR>
<TABLE WIDTH=800 BORDER=0 CELLPADDING=0 CELLSPACING=0 align=center>
<TR>
<TD>
<form name=frm>
<table border=1 width=&quot;95%&quot; bgcolor=darkseagreen align=center class=forumline>
<tr><TH class=thHead colSpan=2 height=25 noWrap align=right><span id=TBlock></span>&nbsp;</th></tr>
<tr><td class=row1 align=center>
<p id=&quot;QArea&quot;>
<BR>
<p id=&quot;finishbuttonp&quot; align=&quot;center&quot;></p>
<script language=&quot;javascript&quot;>
finishbuttonp.innerHTML = &quot;<input type=button name=btnFinish value='Áñ÷ßóôå ôï ÔÅÓÔ' onClick='init()'>&quot;;
</script>
<input type=hidden name=ansNo>
</td></tr>
</table>
</form>

</TD></TR>
</TABLE>



and the xml FILE

<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-7&quot;?>
<data>
<question id=&quot;1&quot;>
<qtext>Ðïéüò åßíáé ï áñéèìüò ôùí ãíùóôþí ÷çìéêþí óôïé÷åßùí ùò ôï 1997</qtext>
<choices>
<choice>120</choice>
<choice>112</choice>
<choice>130</choice>
<choice>92</choice>
</choices>
<answer>2</answer>
</question>
<question id=&quot;2&quot;>
<qtext>Ðïéü ÷çìéêü óôïé÷åßï åéíáé õãñü;</qtext>
<choices>
<choice>Âñþìéï</choice>
<choice>Áæùôï</choice>
<choice>ÍÜôñéï</choice>
<choice>×áëêüò</choice>
</choices>
<answer>1</answer>
</question>
</data>
 
Firstly:
[ignore]
Code:
Your Code Here
[/ignore]


Secondly:
Try just posting the block of code that is causing the error rather than the whole lot.

Thirdly:
If you use <IMG SRC=temp.gif> in XML it will generate two errors. One being that the tag isn't closed, The other being that all attributes have to be surrounded by double quotes.

Lastly:
&quot;i get errors in my code&quot; is not terribly helpful. A description of exactly what error you get would be better.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top