2012-03-19 43 views
1

我还有一个问题源于此前提出的问题: XML - targeting node attribute, push into a Flash AS3 array。我被告知要问一个新问题,而不是更新旧问题。“XML - 针对节点属性,推入Flash AS3阵列”的持续传奇

这是我的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的任何地方访问它们。请记住,每个问题都有两个答案。即:

<答案ID = 'Q1A1' UID = '311288' 的txt = ' '重量=' 0 '/>
<答案ID =' Q1A2' UID = '311289' 的txt =' False'weight ='1'/>

回答

0

我认为有一个更简单的方法来做到这一点。只需引用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)[email protected]); // Question 
    for each (var o:Object in getAnswersByQuestionIndex(1)) { 
     trace([email protected]); // Answers 
    } 
} 

function getQuestionByIndex(index:int):XMLList { 
    return myXML.question.(@id=="Q"+index); 
} 

function getAnswersByQuestionIndex(index:int):XMLList { 
    return getQuestionByIndex(index).answer; 
} 
+0

跟踪输出在第一帧上工作正常。如何在下面的框架6上填充3个文本框? ** question1_txt.htmlText =? answer1_txt.htmlText =? answer2_txt.htmlText =?** – Livi17 2012-03-19 16:41:24

+1

如果你在第一帧定义你的函数并在你进步之前加载你的xml,那么你应该可以调用相同的函数。 question1_txt.htmlText = getQuestionByIndex(1)。@ txt and answer1_txt.htmlText = getAnswersByQuestionIndex(1)[0]。@ txt – Corey 2012-03-19 16:58:09

+0

感谢Corey,这是完美的工作! – Livi17 2012-03-19 17:06:17