我还有一个问题来自于之前提出的问题:XML - targeting node attribute, push into a Flash AS3 array。我被告知要问一个新问题,而不是更新旧问题。
以下是我的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>这就是我所使用的从问题标记获取txt属性的方法。
//load the xml
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("html/BlahBlah/manifest.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
trace(myXML.*);
//.....Your 'myXML' is here....
questions = {};
//Extracting question from xml
for each (var item:XML in myXML.question) {
questions[item. @ id] = item. @ txt;
}
}下面是一个函数位于一个单独的框架上,它扩展了fla的长度。
//Question list
var questions:Object;
//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];
}引用该函数,然后使用它来针对特定的txt属性填充动态textfield:
question1_mc.question_txt.htmlText = "<b>Question 1: </b><br>"+ getQuestionAt(1);我现在需要的是一种从应答标记中获取txt属性值的方法,并能够从fla的任何地方访问它们。记住,每个问题都有两个答案。ie:
<答案id='Q1A1‘uId='311288’txt='True‘权重=’0‘/>
<答案id='Q1A2‘uId='311289’txt='False‘权重=’1‘/>
发布于 2012-03-19 16:24:27
我觉得有个更简单的方法。与其加载XML并将问题存储在对象中,不如直接引用XML对象。XML已经是一个对象,所以没有理由将它解析为另一个对象。另外,你可以根据你的需要提取所有你需要的信息。我建议这样做:
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.addEventListener(Event.COMPLETE, complete);
myLoader.load(new URLRequest("html/BlahBlah/manifest.xml"));
function complete(e:Event):void {
myLoader.removeEventListener(Event.COMPLETE, complete);
myXML = new XML(e.target.data);
//Data for question 1
trace(getQuestionByIndex(1).@txt); // Question
for each (var o:Object in getAnswersByQuestionIndex(1)) {
trace(o.@txt); // Answers
}
}
function getQuestionByIndex(index:int):XMLList {
return myXML.question.(@id=="Q"+index);
}
function getAnswersByQuestionIndex(index:int):XMLList {
return getQuestionByIndex(index).answer;
}https://stackoverflow.com/questions/9772832
复制相似问题