2012-10-23 39 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) { 
     ... 

你的第二個問題:是的,他們都使用的顯示名稱「標籤」。