AS3 XML 到多维数组

发布于 2024-11-15 14:09:58 字数 780 浏览 0 评论 0原文

我到处都看过,虽然有很多解释,但我似乎无法理解它。

这是我的 xml 结构:

 <question> 
            <q1> Who coined the term “Clinical Psychology”? </q1>       
            <answer> Lightner Witmer </answer>

            <option1> Stanley Hall </option1>
            <option2> Lightner Witmer </option2>
            <option3> Henry P. David </option3>
    </question>

我可以循环遍历并挑选出问题和答案,然后将它们放入单独的数组中。我遇到的问题是循环并将选项拉入多维数组中,如下所示:

var one:Array = new Array( 3 ); 
one[0] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];
one[1] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];
one[2] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];

任何帮助将不胜感激。

I've looked everywhere, and although there are many explanations, I can't seem to wrap my head around it.

Here is my xml structure:

 <question> 
            <q1> Who coined the term “Clinical Psychology”? </q1>       
            <answer> Lightner Witmer </answer>

            <option1> Stanley Hall </option1>
            <option2> Lightner Witmer </option2>
            <option3> Henry P. David </option3>
    </question>

I can loop through fine and pick out the questions and answers, then throw them into separate arrays. The problem I'm having is looping and pulling the options into a multidimensional array like such:

var one:Array = new Array( 3 ); 
one[0] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];
one[1] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];
one[2] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゃ懵逼小萝莉 2024-11-22 14:09:58
    var answers:Array = [];

    for(var i = 0; i< xml.question.length; i++){
    var node:Array = [];
    node.push(xml.question[i].option1);
    node.push(xml.question[i].option2);
    node.push(xml.question[i].option3);

//or
    var node:Array = [xml.question[i].option1,xml.question[i].option2,xml.question[i].option3];

    answers.push(node);

    }

现在可以检索问题的答案:

answers[questionIndex][answerIndex];

希望有帮助

    var answers:Array = [];

    for(var i = 0; i< xml.question.length; i++){
    var node:Array = [];
    node.push(xml.question[i].option1);
    node.push(xml.question[i].option2);
    node.push(xml.question[i].option3);

//or
    var node:Array = [xml.question[i].option1,xml.question[i].option2,xml.question[i].option3];

    answers.push(node);

    }

answers for questions can now be retrieved:

answers[questionIndex][answerIndex];

Hope that helps

丶视觉 2024-11-22 14:09:58

我不确定您是否以您想要的形式设置数组,但是,请关闭您发布的代码。
你尝试过吗?

var one:Array = new Array( 3 ); 
one[0] = new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );
one[1] = new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );
one[2] = new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );

对于这样的东西,我更喜欢有一个以对象作为元素的问题数组。每个对象都会保存有关该问题的所有信息。这将允许为每个问题分配一个自定义类别。或者可以使用基本的动态对象

var questions:Array = new Array();

var obj:Object = new Object();
obj.question = "Who coined the term 'Clinical Psychology'?"
obj.correctAnswer = "Lightner Witmer";
obj.possibleAnswers =new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );

questions.push( obj );

I am not sure you are setting up the array in the form you want but, going off the code you posted.

Have you tried?

var one:Array = new Array( 3 ); 
one[0] = new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );
one[1] = new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );
one[2] = new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );





With something like this I would prefer to have a questions array with objects as the elements. Each object would hold all the info about that question. This would allow for a custom class to be assigned for each question. Or a basic dynamic object could be used

var questions:Array = new Array();

var obj:Object = new Object();
obj.question = "Who coined the term 'Clinical Psychology'?"
obj.correctAnswer = "Lightner Witmer";
obj.possibleAnswers =new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );

questions.push( obj );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文