Right now, I have an XML file containing data for multiple choice questions for a quiz as seen below. I intend to have lots of questions in here so that the user will get different questions each time they play, which are parsed from the XML file. The quiz itself contains 15 questions each time it is run, so it parses the XML file for questions. Here's an example of the contents of the XML file:
<?xml version="1.0" encoding="utf-8"?>
<questionData>
<question>
<level> 1 </level>
<ref> ref1 </ref>
<questionText>This is a test question using XML!</questionText>
<A>This is A using XML!</A>
<B>This is B using XML!</B>
<C>This is C using XML!</C>
<D>This is D using XML!</D>
<corrAnswer> A </corrAnswer>
<APerc> 97 </APerc>
<BPerc> 1 </BPerc>
<CPerc> 1 </CPerc>
<DPerc> 1 </DPerc>
<PAFAnswer> A </PAFAnswer>
<PAFFeeling> Sure </PAFFeeling>
<FF> B </FF>
<FFcorrPerc> 100 </FFcorrPerc>
<FFwrongPerc> 0 </FFwrongPerc>
<FFPAFAnswer> A </FFPAFAnswer>
<FFPAFFeeling> Sure </FFPAFFeeling>
<EXP> This is a test explanation for the info box. </EXP>
<pronuns> pro-nun-cee-a-sh-un </pronuns>
</question>
<question>
<level> 2 </level>
<ref> ref2 </ref>
<questionText>This is another test question using XML!</questionText>
<A>This is A2 using XML!</A>
<B>This is B2 using XML!</B>
<C>This is C2 using XML!</C>
<D>This is D2 using XML!</D>
<corrAnswer> B </corrAnswer>
<APerc> 96 </APerc>
<BPerc> 1 </BPerc>
<CPerc> 1 </CPerc>
<DPerc> 2 </DPerc>
<PAFAnswer> B </PAFAnswer>
<PAFFeeling> Sure </PAFFeeling>
<FF> A </FF>
<FFcorrPerc> 100 </FFcorrPerc>
<FFwrongPerc> 0 </FFwrongPerc>
<FFPAFAnswer> B </FFPAFAnswer>
<FFPAFFeeling> Sure </FFPAFFeeling>
<EXP> This is another test explanation for the info box. </EXP>
<pronuns> pro-nun-cee-a-sh-un two </pronuns>
</question>
As you can see above, at the moment I have two questions. However, the problem itself doesn't actually reside with the XML file, in fact it resides code-side in C++/CLI. The code I'm using for my parsing function is below:
XmlTextReader^ dataFromQFile = gcnew XmlTextReader("Millionaire\\questionsData.xml");
XmlDocument^ questionsData = gcnew XmlDocument();
questionsData->Load("Millionaire\\questionsData.xml");
XmlNode^ QuestionXML = questionsData->FirstChild;
for each (QuestionXML in questionsData->GetElementsByTagName("question"))
{
levelFromXML = QuestionXML["level"]->InnerText;
questionFromXML = QuestionXML["questionText"]->InnerText;
AFromXML = QuestionXML["A"]->InnerText;
BFromXML = QuestionXML["B"]->InnerText;
CFromXML = QuestionXML["C"]->InnerText;
DFromXML = QuestionXML["D"]->InnerText;
corrFromXML = QuestionXML["corrAnswer"]->InnerText;
APercFromXML = QuestionXML["APerc"]->InnerText;
BPercFromXML = QuestionXML["BPerc"]->InnerText;
CPercFromXML = QuestionXML["CPerc"]->InnerText;
DPercFromXML = QuestionXML["DPerc"]->InnerText;
phoneAnswerFromXML = QuestionXML["PAFAnswer"]->InnerText;
phoneFeelingFromXML = QuestionXML["PAFFeeling"]->InnerText;
fiftyAnswer = QuestionXML["FF"]->InnerText;
fiftyCorrPerc = QuestionXML["FFcorrPerc"]->InnerText;
fiftyWrongPerc = QuestionXML["FFwrongPerc"]->InnerText;
fiftyPhoneAnswer = QuestionXML["FFPAFAnswer"]->InnerText;
fiftyPhoneFeeling = QuestionXML["FFPAFFeeling"]->InnerText;
exp = QuestionXML["EXP"]->InnerText;
pronuns = QuestionXML["pronuns"]->InnerText;
if (Int32::Parse(levelFromXML) == QuestionNo)
{
return;
}
Now, what I'm doing here is parsing the file for all the different elements and storing them in the above variables. For the if statement, I'm saying that if the level tag is equal to the questionNo variable, return the function (this is because the questions get harder the more you progress, so if level = 2 and you're on question 2, load that question and keep the variables to their currents values - a rather cheeky trick) and don't parse the file again. However, obviously this creates a problem. Every time the user answers a question correctly, it has to search for a question that has the same value as questionNo (so if they answered question 2 correctly, questionNo willnow = 3, hence it has to reparse the whole file for a question that has level = 3) Due to the volume of questions there will be, parsing the file every time for a new question at runtime will cause significant backlog in the program while it parses the XML file again. So I came up with the idea of storing all the variable values in one member of an array - which obviously again, causes a problem. My question is, do any of you have a better solution to the aforementioned issue or perhaps have a more efficient way of performing this? I would also appreciate if you could provide some code in context so I have a rough idea of what I'm working with. Thanks.
Aucun commentaire:
Enregistrer un commentaire