2012-10-23 36 views
0

在我继承的项目上,我需要从使用Spark List组件切换到使用mx Tree组件,以便能够将项目分组到目录中。我对Flex/XML非常生疏,所以我想知道如果我能够正确地指导如何处理这个问题。为Flex树组件解析XML

我的问题(详细信息/数据如下细节):

  • 我如何检测 'studentGroup' 节点从 '学生' 的节点?

  • 树组件需要一个字段用于显示名称。我的 必须在'studentGroup'节点和'学生' 节点之间有一个共同名称吗?

  • 我完全做错了吗?

以前我的XML数据持平(我已经剥离出来为清晰起见所有的细节):

<studentList> 
    <student> 
     <studentName>Sam</studentName> 
    </student> 
    <student> 
     <studentName>Ruby</studentName> 
    </student> 
</studentList> 

新格式组和学生个人的混合体:

<studentList> 
    <studentGroup> 
     <studentGroupName>Chess</studentGroupName> 
      <student> 
       <studentName>Betty</studentName> 
      </student> 
     </studentGroup> 
    <student> 
     <studentName>Sam</studentName> 
    </student> 
    <student> 
     <studentName>Ruby</studentName> 
    </student> 
</studentList> 

XML目前正在使用此(再次简化)代码进行分析:

for each (var prop:XML in studentsXML.file){ 
    tempArray = new ArrayCollection(); 
    for each(var studentProp:XML in prop.studentList.student){ 
     tempStudent = new Student(studentProp.studentName); 
     tempArray.addItem(tempStudent); 
    } 
} 

我需要改变它,以便'studentGroups'我做一件事,'学生'我像上面那样处理它。在伪代码中,它看起来像下面的内容,但是我正在语法上跳动(或者我完全偏离了轨道?)。

for each (var prop:XML in studentsXML.file){ 
    tempArray = new ArrayCollection(); 
    for each(var studentProp:XML in prop.studentList){ 

     //HOW DO I DETECT A StudentGroup FROM A Student NODE? 

     if (studentList.studentGroup){ 
      //student group 
      tempStudentGroup = new StudentGroup(studentProp.studentGroupName); 
      for each(var student:XML in studentList.studentGroup){ 
       tempStudent = new Student(studentProp.studentName); 
       tempStudentGroup.add(tempStudent); 
      } 

      tempArray.addItem(tempStudentGroup); 
     }else{ 
      //single student 
      tempStudent = new Student(studentProp.studentName); 
      tempArray.addItem(tempStudent); 
     } 
    } 
} 

回答

0

我会尝试这样的事:

for each(var studentGroup:XML in prop.studentGroup) 
{ 
    //student group 
    for each(var student:XML in studentGroup.student) { 
     tempStudent = new Student(studentProp.studentName); 
     tempStudentGroup.add(tempStudent); 
    } 
    tempArray.addItem(tempStudentGroup); 
} 
for each(var student:XML in prop.student) 
{ 
    //single student 
    tempStudent = new Student(studentProp.studentName); 
    tempArray.addItem(tempStudent); 
} 
0

如果想在MX使用:树,你的XML将是这样的:

<studentList> 
    <student label="Chess"> 
    <student label="Bett"/> 
    </student> 
    <student label="Sam"/> 
    <student label="Ruby"/> 
</studentList> 

这意味着:你应该总是使用唯一的字符串将内容包含在任何深度中,并且mx:Tree 会将这些内容显示为节点父节点或节点,具体取决于哪些节点具有子节点。

第一个问题:您可以选中“hasChildren”来区分itemRenderer中的学生组和学生。像这样:

override public function set data(value:Object):void { 
    if(value != null) { 
    super.data = value; 

    if(TreeListData(super.listData).hasChildren) { 
     ... 

你的第二个问题:是的,他们都使用的显示名称“标签”。