目前,我正在使用XML来创建一个包含许多问题的数据结构,用于测验,其中包含问题本身,答案等信息,然后将这些问题放入要显示的变量中。标签。这是XML文件的一个示例:
<?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>
</questionData>
现在,我打算在此文件中包含大量问题。然而,你进步的测验越进一步,他们就越难获得,因此我添加了一个名为level的元素,其数字对应于何时被询问(例如,你在测验中有15个问题,你有问题3因此它解析XML文件以获得level元素为3的问题,然后将数据加载到程序中。
然而,存在一个问题 - 很可能是逻辑错误。在C ++ / CLI代码中,我有:
XmlTextReader^ dataFromQFile = gcnew XmlTextReader("Millionaire\\questionsData.xml");
XmlDocument^ questionsData = gcnew XmlDocument();
questionsData->Load("Millionaire\\questionsData.xml");
XmlElement^ root = questionsData->DocumentElement;
XmlNodeList^ listOfQuestions = root->GetElementsByTagName("questionData");
XmlNodeList^ Questions = root->GetElementsByTagName("question");
for each (Questions in listOfQuestions)
{
levelFromXML = (root->GetElementsByTagName("level"))->Item(0)->InnerText;
questionFromXML = (root->GetElementsByTagName("questionText"))->Item(0)->InnerText;
AFromXML = (root->GetElementsByTagName("A"))->Item(0)->InnerText;
BFromXML = (root->GetElementsByTagName("B"))->Item(0)->InnerText;
CFromXML = (root->GetElementsByTagName("C"))->Item(0)->InnerText;
DFromXML = (root->GetElementsByTagName("D"))->Item(0)->InnerText;
corrFromXML = (root->GetElementsByTagName("corrAnswer"))->Item(0)->InnerText;
APercFromXML = (root->GetElementsByTagName("APerc"))->Item(0)->InnerText;
BPercFromXML = (root->GetElementsByTagName("BPerc"))->Item(0)->InnerText;
CPercFromXML = (root->GetElementsByTagName("CPerc"))->Item(0)->InnerText;
DPercFromXML = (root->GetElementsByTagName("DPerc"))->Item(0)->InnerText;
phoneAnswerFromXML = (root->GetElementsByTagName("PAFAnswer"))->Item(0)->InnerText;
phoneFeelingFromXML = (root->GetElementsByTagName("PAFFeeling"))->Item(0)->InnerText;
fiftyAnswer = (root->GetElementsByTagName("FF"))->Item(0)->InnerText;
fiftyCorrPerc = (root->GetElementsByTagName("FFcorrPerc"))->Item(0)->InnerText;
fiftyWrongPerc = (root->GetElementsByTagName("FFwrongPerc"))->Item(0)->InnerText;
fiftyPhoneAnswer = (root->GetElementsByTagName("FFPAFAnswer"))->Item(0)->InnerText;
fiftyPhoneFeeling = (root->GetElementsByTagName("FFPAFFeeling"))->Item(0)->InnerText;
exp = (root->GetElementsByTagName("EXP"))->Item(0)->InnerText;
pronuns = (root->GetElementsByTagName("pronuns"))->Item(0)->InnerText;
}
当我尝试遍历问题列表时(questionData是父项,问题是孩子,问题包含的所有数据都是子项),当我尝试在标签中显示变量时,我得不到任何文本因此,让我相信我在循环中犯了一个逻辑错误。我对XML比较新,所以我无法发现错误。您是否有人能够帮助我或者可能知道更有效的方式来执行上述任务?感谢。
答案 0 :(得分:1)
您的根元素(分别是documentElement)已经是questionData
元素,因此执行root->GetElementsByTagName("questionData")
会返回一个空列表。如果你想在循环中处理question
元素,那么只需处理for each(question in questionsData->GetElementsByTagName("question"))
然后在循环内部确保你在循环变量上调用方法,例如levelFromXML = (question->GetElementsByTagName("level"))->Item(0)->InnerText;
。
我还会在循环中使用https://msdn.microsoft.com/en-us/library/sss31aas%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1而不是GetElementsByTagName
,因此您只需使用levelFromXML = question["level"]->InnerText;
替换上述代码段。