XML - 瞄准节点属性,推到Flash AS3数组数组、节点、推到、属性

2023-09-09 21:55:48 作者:韶华如梦

我想在每一个问题标签中的TXT属性的内容之外推入命名为AS3闪光问题的数组。下面是我的XML文件的摘录。

 <问题ID =Q1UID ='99036'no_ans ='2'的txt ='。在一个平面结构的员工预计不会提供他们的老板有自己的意见反馈=''类型='MC'passingWeight ='1'URL ='媒体/'>
    <解答ID ='Q1A1'UID ='311288'的txt ='真'的重量=0/>
    <解答ID ='Q1A2'UID ='311289'的txt ='假'重=1/>
< /问题>
<问题ID ='Q2'UID ='99037'no_ans ='2'的txt ='在一个层次,信息通常向下流动。反馈=''类型='MC'passingWeight ='1'URL ='媒体/'>
    <解答ID ='Q2A1'UID ='311290'的txt ='真'重=1/>
    <解答ID ='Q2A2'UID ='311291'的txt ='假'重=0/>
< /问题>
<问题ID ='Q3'UID ='99038'no_ans ='2'的txt ='。有人谁使许多项目将在同一时间是别人的一个例子谁是灵活的时间为本反馈=''类型='MC'passingWeight ='1'URL ='媒体/'>
    <解答ID ='Q3A1'UID ='311292'的txt ='真'重=1/>
    <解答ID ='Q3A2'UID ='311293'的txt ='假'重=0/>
< /问题>
 

下面是我尝试在一个循环:

  //获取问题数
    跟踪(myXML.question.length());
    numberOfQuestions = myXML.question.length();

    //循环,并推动问题到问题阵列顶部
    对于(VAR我:= 0; I< numberOfQuestions;我++){
        跟踪(你好);
        questions.push(myXML.question @ TXT);
        追踪(问题);
    }
 

这只是全押9的问题一下子到阵列中的每个位置。我想每个阵列位置的1个问题。我不知道如何使用的问题标签的id属性来区分每一个问题。

编辑:我尝试这样做,我可以使用 getQuestionAt(2)在的processXML 函数中,而不是外部访问的问题文本它。

  VAR与myXML:XML;
VAR myLoader:的URLLoader =新的URLLoader();
myLoader.load(新的URLRequest(HTML / VUBZ7318CROSSCULTUREQUIZ / manifest.xml文件));
myLoader.addEventListener(引发Event.COMPLETE,的processXML);
功能的processXML(五:事件):无效{
    与myXML =新的XML(e.target.data);

    //trace(myXML.question)

    //获取问题数
    跟踪(myXML.question.length());
    numberOfQuestions = myXML.question.length();

    //问题列表
    VAR的问题:对象= {};
    //从提取XML问题
    每个(VAR项目:XML在myXML.question){
        问题[项目。 @ ID] =项。 @ TXT;
    }
    //一些方法从问题列表中获取问题
    功能getQuestionAt(索引:号码):字符串{
        如果(问题[Q+指数] == undefined)的{
            抛出新的错误(错误的索引问题!!!);
        }
        回到问题[Q+指数]
    }

    //获取从列表中的问题
    跟踪(这是没有问题2:\ t+ getQuestionAt(2));


}
 
flash as3 使要拖拽的东西紧贴线

解决方案

创建新的图层,其中只有一帧,使该帧的长度,只要您的总帧(例如6长)。然后把这个code在该帧。

  //问题列表
VAR的问题:对象;
//一些方法从问题列表中获取问题
功能getQuestionAt(索引:号码):字符串{
    如果(问题[Q+指数] == undefined)的{
        抛出新的错误(错误的索引问题!!!);
    }
    回到问题[Q+指数]
}
 

然后这些行添加到您的的processXML 功能

 函数的processXML():* {
 //.....Your与myXML在这里....
 题= {};
 //从提取XML问题
 每个(VAR项目:XML在myXML.question){
    问题[项目@ ID =项目@ TXT。
 }
}
 

呼叫 getQuestionAt ,只要你想得到的问题。你可以调用该函数在任何帧,因为它是看得见的所有帧。

I'm trying to push just the contents of the "txt" attribute in each "question" tag into an array named "questions" in AS3 Flash. Here is an excerpt from my xml file.

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

Here is my attempt at a loop:

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

This just pushes all 9 of the questions at once into each position of the array. I wanted 1 question per array position. I'm not sure how to use the id attribute in the question tag to differentiate each question.

EDIT: I tried this and I can access the questions texts using getQuestionAt(2) from within the processXML function, but not outside of it.

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


}

解决方案

Create new layer, which have only one frame, and make that frame's length as long as your total frames are(for instance 6 as long). Then put this code in that frame.

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

Then add these lines into your processXML function

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

Call getQuestionAt whenever you want to get questions. You can call that function in any frame, because it's 'visible' on all frames.