【问题标题】:XML - targeting node attribute, push into a Flash AS3 arrayXML - 定位节点属性,推入 Flash AS3 数组
【发布时间】:2012-04-01 10:42:12
【问题描述】:

我试图将每个“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 属性来区分每个问题。

编辑: 我试过了,我可以使用 getQuestionAt(2) 从 processXML 函数内部访问问题文本,但不能在外部。

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


}

【问题讨论】:

    标签: xml actionscript-3 flash


    【解决方案1】:

    创建只有一帧的新图层,并使该帧的长度与您的总帧数一样长(例如 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。您可以在任何帧中调用该函数,因为它在所有帧上都是“可见的”。

    【讨论】:

    • 如何从 fla 的不同框架访问其中一个问题?我尝试在第 6 帧上使用 text_txt.htmlText = getQuestionAt(2); 填充文本字段,但它不起作用。
    • 创建一个只有一帧的新层,并使该帧的长度与您的总帧数一样长(例如6个)。然后在该框架中添加 AS 代码。在这种情况下,您可以在任何帧中调用该函数,因为它在所有帧上都是“可见的”。
    • 我尝试了你的建议,但它仍然不起作用......是因为我将你的代码放在 processXML() 函数中吗?请参阅我上面的编辑。它在函数内部工作,但不在函数外部,也不能从 fla 的任何其他地方工作。
    • 现在可以使用了。我将所有游戏测验动作脚本放在一个框架上,然后在一个跨所有框架的单独框架中放置 getQuestionAt() 函数。我可以使用这个填充所有问题:question1_mc.question1_txt.htmlText = getQuestionAt(2);
    • 我试图对答案文本做同样的事情,但得到一个错误...请参阅上面我最初的帖子中的 “EDIT 2”。
    【解决方案2】:

    您的 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 )
    }
    

    【讨论】:

    • 另外,请永远不要使用“NEST”功能,这很糟糕。
    • 这只是xml的摘录,它确实有一个根节点并且格式正确。 xml 文件太长,包含太多不相关的数据,无法全部发布。
    • 我试过这个,但它给出了以下错误。 TypeError:错误 #1034:类型强制失败:无法将 Object@29328c41 转换为 XMLList。
    【解决方案3】:

    你拥有的不是 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();
    }
    

    【讨论】:

    • 我的客户在 Flash elearningbrothers.com/product_demos/004_flashgames/… 中购买了一个问答游戏,我正在尝试使其成为动态的……它目前不是动态的。我不想重新创建整个事情。 CMS 当前在 xml 文件中生成测验问题。我只需要在第一帧加载问题和答案,并能够在整个 fla 文件的其他帧中访问它们。我不能使用外部 .as 文件,所以所有代码大部分都是内部的。
    • 是什么阻止了您用作文件?但即便如此,您也可以更改我给您的内容以使用框架脚本(即使这样做会让您感到有些不适……我完全明白,因为我现在每天都在使用 AS2)。跨度>
    猜你喜欢
    • 2012-04-04
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多