On a project I've inherited I need to switch from using a spark List component to using an mx Tree component in order to be able to group items into directories. I'm pretty rusty with Flex/XML so I'm wondering if I can get a nudge in the right direction for how to handle this.
My questions (details/data specifics below):
How do I detect a 'studentGroup' node from a 'student' node?
The Tree Component needs a field to use for the display name. Do I have to have a common name between 'studentGroup' nodes and 'student' nodes?
Am I completely doing this wrong?
Previously my XML data was flat (I've stripped out all the detail for clarity):
<studentList>
<student>
<studentName>Sam</studentName>
</student>
<student>
<studentName>Ruby</studentName>
</student>
</studentList>
New format is a mix of groups and individual students:
<studentList>
<studentGroup>
<studentGroupName>Chess</studentGroupName>
<student>
<studentName>Betty</studentName>
</student>
</studentGroup>
<student>
<studentName>Sam</studentName>
</student>
<student>
<studentName>Ruby</studentName>
</student>
</studentList>
The XML is currently being parsed with this (again simplified) code:
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);
}
}
I need to change it so that for 'studentGroups' I do one thing and for 'students' I handle it like above. In pseudo code it would look like below but I am tripping on syntax (or maybe I am completely off track?).
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);
}
}
}