【问题标题】:Parsing XML for Flex Tree component为 Flex Tree 组件解析 XML
【发布时间】:2012-10-13 16:10:42
【问题描述】:

在我继承的项目中,我需要从使用 spark List 组件切换到使用 mx Tree 组件,以便能够将项目分组到目录中。我对 Flex/XML 很生疏,所以我想知道我是否可以在正确的方向上推动如何处理这个问题。

我的问题(详情/数据详情如下):

  • 如何从“student”节点检测到“studentGroup”节点?

  • 树组件需要一个字段用于显示名称。难道我 在“studentGroup”节点和“student”之间必须有一个通用名称 节点?

  • 我完全做错了吗?

以前我的 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”做一件事,而为“students”我像上面一样处理它。在伪代码中它看起来像下面,但我在语法上绊倒了(或者我完全偏离了轨道?)。

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

【问题讨论】:

    标签: actionscript-3 apache-flex


    【解决方案1】:

    如果想在 mx:Tree 中使用,你的 xml 会是这样的:

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

    这意味着:您应该始终使用唯一的字符串来包含任何深度的内容,并且 mx:Tree 将这些显示为节点父节点或节点,取决于哪个有子节点。

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

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

    您的第二个问题:是的,它们都使用“标签”作为显示名称。

    【讨论】:

      【解决方案2】:

      我会尝试这样的:

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

      【讨论】:

        猜你喜欢
        • 2014-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多