2012-03-15 39 views
0

我试图将每个“问题”标签中的“txt”属性的内容推入AS3 Flash中名为“questions”的数组中。这是我的xml文件的摘录。XML - 针对节点属性,推入Flash AS3阵列

<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([email protected]); 
     trace(questions); 
    } 

这只是推的问题,所有9日一旦进入阵列的每个位置。我想每个阵列位置1个问题。我不确定如何在问题标记中使用id属性来区分每个问题。

编辑:我尝试这样做,我可以从的processXML函数中(2)访问使用getQuestionAt问题文本,但不应超出它。

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


} 

回答

0

创建只有一个帧的新层,并且只要您的总帧数(例如6个长),就创建该帧的长度。然后把这个代码放在那个框架中。

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

然后,每当你想提问添加这些行到您的processXML功能

function processXML():*{ 
//.....Your 'myXML' is here.... 
questions = {}; 
//Extracting question from xml 
for each(var item:XML in myXML.question){ 
    questions[[email protected]] = [email protected]; 
} 
} 

呼叫getQuestionAt。您可以在任何框架中调用该功能,因为它在所有框架上都是“可见的”。

+0

我如何进入的问题之一来自不同帧的FLA? 我试图在第6帧使用** text_txt.htmlText = getQuestionAt(2); **来填充文本字段,但它不起作用。 – Livi17 2012-03-15 21:40:10

+0

创建只有一个帧的新图层,并且只要您的总帧数(例如6个)就可以创建该帧的长度。然后在该框架中添加AS代码。在这种情况下,您可以在任何帧中调用该函数,因为它在所有帧上都是“可见的”。 – Engineer 2012-03-15 21:53:15

+0

我试过你的建议,但它仍然无法工作......是因为我把你的代码放在** processXML()**函数中?看到我上面的编辑。它在函数内部工作,但不在外部,或从fla中的任何其他地方工作。 – Livi17 2012-03-15 22:06:42

0

您的XML设置只是一个错误。在AS3中,你需要一个根节点。根节点不可访问,它只是一种包装。在你的情况下,问题是你的根节点不可访问,这将使这些属性无法访问。所以把你的XML包装。我可能是错误的无法访问根节点属性,但我对你的XML是正确的。而添加包装只是使得更容易。

<questions> 
    <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> 
</questions> 

然后像这样获取属性。

var questions:XMLList = new XMLList(e.target.data.question) 
for each (var question:XML in questions){ 
    trace([email protected]) 
} 
+0

也请永远不要“NEST”功能它只是不好。 – 2012-03-16 01:00:07

+0

这只是xml的摘录,它确实有一个根节点并且格式正确。 XML文件太长,并包含太多不相关的数据来发布。 – Livi17 2012-03-16 01:23:52

+0

我试过这个,但是它给出了下面的错误。 TypeError:错误#1034:类型强制失败:无法将Object @ 29328c41转换为XMLList。 – Livi17 2012-03-19 15:05:25

0

你有什么不是XML,而是一个XMLList,它是完全可以接受的。

你不需要循环。你可以像这样得到另一个XMLList。 XMLList就像一个XML数组,但在这种情况下,您将不会有完全形成的节点,而只是所有属性的内容。

它会是这样的:

var questionTxt:XMLList = [email protected];//yourQuestions contains your originalXMLList as shown above 

现在,您可以访问每一个文本元素为:

var stem:String = String(questionTxt[0]); 

如果由于某种原因,你绝对必须有一个数组,您可以这样做:

var questions:Array = new Array(); 
for (var i:int = 0; i< questionTxt.length(); i++) { 
    questions[i] = questionTxt[i]; 
} 

但是,它看起来像很多o因为你可以简单地使用XMLList,就像通过e4x访问它一样。你的全部目标是什么?

我只是看着你的问题多一点细心,你真正需要做的是这样的:

protected var questions:XMLList; 
public function processXML(e:Event):void { 
      myXML = XML(e.target.data); 
      questions = myXML.question; 

      // get number of questions 
      trace(myXML.question.length()); 
} 

public function getQuestionAt(index:Number):String { 
      if (questions[index] == undefined) { 
       throw new Error("Wrong index for question!!!"); 
      } 
      return questions[index].attribute('txt'); 
}  

public function get numberOfQuestions():int { 
    return myXML.question.length(); 
} 
+0

我的客户有闪光灯 http://elearningbrothers.com/product_demos/004_flashgames/002_gameshowdemo/demo.htm ,我试图让动态...它是目前还没有一个动态购置问答游戏。我不想重新创建整个事物。 CMS目前在xml文件中生成测验问题。我只需要在第一个框架中加载问题和答案,并且能够在整个fla文件的其他框架中访问它们。我不能使用外部.as文件,所以所有代码都是内部的。 – Livi17 2012-03-16 01:47:05

+0

什么阻止你使用文件?但即使如此,你也可以改变我给你的框架脚本(即使它让你感觉有点不舒服),我完全明白,因为我现在每天都在使用AS2。 – 2012-03-16 01:50:08