XML - 目标节点属性,推入Flash AS3阵列

时间:2012-03-15 20:49:11

标签: xml actionscript-3 flash

我正在尝试将每个“question”标记中“txt”属性的内容推送到AS3 Flash中名为“questions”的数组中。这是我的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>

这是我尝试循环:

// 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(myXML.question.@txt);
        trace(questions);
    }

这只是将所有9个问题同时推送到数组的每个位置。我想要每个阵列位置1个问题。我不确定如何在问号标签中使用id属性来区分每个问题。

编辑:我试过这个,我可以使用 processXML 函数中的 getQuestionAt(2)来访问问题文本,但不能在外面它的。

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


}

3 个答案:

答案 0 :(得分: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[item.@id] = item.@txt;
 }
}   

每当您想要提问时,请致电getQuestionAt。您可以在任何帧中调用该函数,因为它在所有帧上都是“可见的”。

答案 1 :(得分: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( question.@txt )
}

答案 2 :(得分:0)

你所拥有的不是XML,而是XMLList,它是完全可以接受的。

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

它会是这样的:

var questionTxt:XMLList = yourQuestions.@txt;//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];
}

然而,看起来很多工作都没有,因为你可以通过e4x访问它来简单地使用XMLList。你的全部目标是什么?

我只是仔细看了一下你的问题,而你真正需要做的就是:

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