【发布时间】:2012-04-01 10:42:12
【问题描述】:
我试图将每个“question”标记中的“txt”属性的内容推送到 AS3 Flash 中名为“questions”的数组中。这是我的 xml 文件的摘录。
<question id='Q1' uId='99036' no_ans='2' txt='In a flat structure employees are not expected to provide their bosses with their opinions.' feedback='' type='MC' passingWeight='1' url='media/'>
<answer id='Q1A1' uId='311288' txt='True' weight='0'/>
<answer id='Q1A2' uId='311289' txt='False' weight='1'/>
</question>
<question id='Q2' uId='99037' no_ans='2' txt='In a hierarchy, information typically flows downward.' feedback='' type='MC' passingWeight='1' url='media/'>
<answer id='Q2A1' uId='311290' txt='True' weight='1'/>
<answer id='Q2A2' uId='311291' txt='False' weight='0'/>
</question>
<question id='Q3' uId='99038' no_ans='2' txt='Someone who keeps many projects going at one time is an example of someone who is flexible-time oriented.' feedback='' type='MC' passingWeight='1' url='media/'>
<answer id='Q3A1' uId='311292' txt='True' weight='1'/>
<answer id='Q3A2' uId='311293' txt='False' weight='0'/>
</question>
这是我的循环尝试:
// get number of questions
trace(myXML.question.length());
numberOfQuestions = myXML.question.length();
//loop and push questions into questions array at top
for (var i:int = 0; i < numberOfQuestions; i++) {
trace("Hello.");
questions.push(myXML.question.@txt);
trace(questions);
}
这只是将所有 9 个问题一次推送到数组的每个位置。我想要每个数组位置 1 个问题。我不确定如何使用问题标签中的 id 属性来区分每个问题。
编辑: 我试过了,我可以使用 getQuestionAt(2) 从 processXML 函数内部访问问题文本,但不能在外部。
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("html/VUBZ7318CROSSCULTUREQUIZ/manifest.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
//trace(myXML.question)
// get number of questions
trace(myXML.question.length());
numberOfQuestions = myXML.question.length();
//Question list
var questions:Object = {};
//Extracting question from xml
for each (var item:XML in myXML.question) {
questions[item. @ id] = item. @ txt;
}
//Some method for fetching question from question list
function getQuestionAt( index:Number ):String {
if (questions["Q" + index] == undefined) {
throw new Error("Wrong index for question!!!");
}
return questions["Q"+index];
}
//Getting question from list
trace( "Here is question No 2:\t" + getQuestionAt(2) );
}
【问题讨论】:
标签: xml actionscript-3 flash