Flex中的动态问卷

时间:2011-04-02 12:17:45

标签: xml flex

我想在Flex 3中为调查问卷制作一个模板,该模板读取动态XML文件并创建调查问卷。示例性XML:

<test>

<question>
  <q>Who was born earlier?</q>
  <answer value="true">Vincent van Gogh</answer>
  <answer value="false">Piet Mondrian</answer>
</question>

<question>
  <q>What color is Dutch national flag?</q>
  <answer value="false">blue, red and green</answer>
  <answer value="false">green, red and white</answer>
  <answer value="true">blue, red and white</answer>
</question>

<question>
  <q>Which season is the coldest?</q>
  <answer value="false">fall</answer>
  <answer value="true">winter</answer>
  <answer value="false">spring</answer>
  <answer value="false">summer</answer>
</question>

</test>

问题和答案的数量可能会有所不同。计划是使用带有单选按钮的嵌套转发器(一个用于问题,另一个用于答案)。我可以将所有的question.q保存到一个ArrayCollection中,但是如果每个问题中只有少数几个具有相同的“答案”标记,我应该如何处理我的答案呢?如何访问每个属性的“value”属性,以检查用户是否选择了正确的答案?

1 个答案:

答案 0 :(得分:1)

您需要从XML创建域模型。不要跳过这一步,因为它比你想象的更容易做,更直接。首先创建一个简单的类:

public class Question {
   public var question : String;
   public var answers : ArrayCollection = new ArrayCollection();

   public Question( node : XML ) {
      question = node.q.text();
      for each( var answer : XML in question.answer ) {
          answers.addItem( new Answer( answer ) );
      }
   }
}

public class Answer {
   public var text : String;
   public var correct : Boolean;

   public Answer( node : XML ) {
      text = node.text();
      correct = Boolean(node.@value);
   }
}

填充节点的ArrayCollection,如下所示:

var questions = new ArrayCollection();
for each( var node : XML in xml.question ) {
   questions.addItem( new Question( node ) );
}

大概就是这样。然后你的问题数组可以作为转发器的dataProvider。而question.answers可以作为内置中继器的中继器。