我正在尝试从一个以临时自定义XML格式设置的文本文件中提取数据:
<question>
<question_text>What should you do with your work when traveling?</question_text>
<answer_correct>Keep it in somewhere locked and out of view</answer_correct>
<answer_incorrect>Put it in the back seat so it's off your mind</answer_incorrect>
<answer_incorrect>Put it in the front seat so it's easy to access</answer_incorrect>
<answer_incorrect>Throw it out the window</answer_incorrect>
<reason><p>PCWorld reported in 2014 that 31% of breaches are due to lost or stolen devices. Hiding or locking away confidential materials and equipment is the best solution to prevent loss or theft.</p></reason>
</question>
我能够在JSFIDDLE http://jsfiddle.net/bhwxp4Lb/2/中正确解析数据:
for(var i=0;i<data.length;i++){
if(data[i] == "<question>"){
i++;
a = "<question_text>";
b = data[i];
questions.push(b.substr(a.length,(b.length - (a.length+1))));
i++;
a = "<answer_correct>";
b = data[i];
answerText.push(b.substr(a.length,(b.length - (a.length+1))));
i++;
a = "<answer_incorrect>";
b = data[i];
answerText.push(b.substr(a.length,(b.length - (a.length+1))));
i++;
a = "<answer_incorrect>";
b = data[i];
answerText.push(b.substr(a.length,(b.length - (a.length+1))));
i++;
a = "<answer_incorrect>";
b = data[i];
answerText.push(b.substr(a.length,(b.length - (a.length+1))));
i++;
a = "<reason>";
b = data[i];
reasons.push(b.substr(a.length,(b.length - (a.length+1))));
i++;
answers.push(answerText);
}
}
但是,它无法在我的本地环境中正常运行。我假设它必须对字符串中的less than
(&lt;)和greater than
(&gt;)符号执行某些操作;例如:if(data[I] == <question>)
在JSFIDDLE中按预期工作,但在我的本地系统上没有。有谁知道如何解决这个问题?我这样做了吗?有没有更好的办法?当我正在比较的字符串有符号时,如何进行字符串比较?