“XML - 目标节点属性,推入Flash AS3阵列”的持续传奇

时间:2012-03-19 15:20:28

标签: xml actionscript-3 flash

我还有另一个问题源自此前提出的问题: 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属性以填充动态文本字段:

question1_mc.question_txt.htmlText = "<b>Question 1: </b><br>"+ getQuestionAt(1);

我现在需要的是一种从答案标签中获取txt属性值并从fla中的任何位置访问它们的方法。请记住,每个问题都有两个答案。即:

&LT;回答id ='Q1A1'uId ='311288'txt ='真实'重量='0'/&gt;

&LT;回答id ='Q1A2'uId ='311289'txt ='错误'权重='1'/&gt;

1 个答案:

答案 0 :(得分:0)

我认为有一种更简单的方法可以做到这一点。而不是加载XML并将您的问题存储在Object中,只需引用XML对象即可。 XML已经是一个Object,因此没有理由将其解析为另一个Object。此外,您将能够根据需要提取所需的所有信息。以下是我建议这样做的方法:

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;
}