2012-03-15 43 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