views:

249

answers:

1

I have two xml files, defect. xml and employee.xml. But the files havea common field but with different names in each file. I want both the files to be merged in to a single array collection.

The structure of my defect.xml file is:

<defectList>
    <defect>
           <revId>123</revId> 
           <revType>IQA</revType> 
           <status>Review Pending</status> 
           <assignedTo>Angeline</assignedTo> 
           <loggedBy>chandran</loggedBy> 
           <closedDate>13-10-2009</closedDate> 
   </defect>
   <defect>
           <revId>124</revId> 
           <revType>IQA</revType> 
           <status>Review Pending</status> 
           <assignedTo>Aarthi</assignedTo> 
           <loggedBy>chandran</loggedBy> 
           <closedDate>15-10-2009</closedDate> 
   </defect>

<defectList>

And my employee.xml

 <Employees>
      <employee>
               <employeeId>256148</employeeId> 
               <employeeName>Angeline</employeeName> 
      </employee>
      <employee>
               <employeeId>256158</employeeId> 
               <employeeName>Aarthi</employeeName> 
      </employee>

<Employees>

I get both the xml files in two array collections:

 <mx:Model id="employeeXML" source="assets/employee.xml"/>
 <mx:ArrayCollection id="employeeList" source="{employeeXML.employee}"/>

 <mx:Model id="defectXML" source="assets/defect.xml"/>
 <mx:ArrayCollection id="defectList" source="{defectXML.defect}"/>

when "assignedTo" matches "employeeName", I want the "employeeId" to be added to the defectList array collection. How can I do this? How to iteerate through an array collection?

And how to check if the assignedTo field in defectList equals employeeName field in employeeList? Some one guide me..

EDIT

ok, Now I'm able to compare the two fields,assignedTo of defectList and employeeName of employeeList.Thanks to Simon:

 var defect:Object; 
 var employee:Object; 
for each (defect in defectList) 
{ 
    for each (employee in employeeList) 
    { 
        if(defect.assignedTo == employee.employeeName)
        {
          // defectList.addItem(employee.employeeId);
               // I tried this,but it is wrong .
        }
   } 
}

But how to add the employeeId field to that particular array element,so that I could use defectList as the dataprovider for the datagrid n display employee Id also? Can someone help me with this.

SOLUTION

I have found out the solution. Here is the code:

public function init():void{
   var defect:Object; 
   var employee:Object; 

    for each (defect in defectList) 
    { 
        for each (employee in employeeList) 
        { 
            if(defect.assignedTo == employee.employeeName)
            {
             var id:Object;
             id=employee.employeeId;
             defect["employeeId"]=id; 
            }  
        } 
    }
  }

Now, if I give datafield as "employeeId" in the datagrid with defectList as dataprovider, I get the employee id of the corresponding Employee Name.

+1  A: 

I can answer part of this...

Iterating through an ArrayCollection is easy...

var defect:Object;
var employee:Object;
for each (defect in defectList)
{
    for each (employee in employeeList)
    {
        // do your check here.
    }
}

You need to be careful with iterators like ForEach if you are hanging the contents of the arrays as you iterate. You may want to consider loading up a third array as you go.

I'm not sure exactly how you get at the element values from the XML objects created in your code snippet, but there is a lot of help on the Adobe Livedocs

You might be able to use the fact that an object can have its fields reference in a late-binded way...

var elementContents:String = employee["employeeName"];
Simon
ok, I am able to compare the elements. thats for that. But how to add the field "EmployeeId" to the defectList array collection? Any idea?
Angeline Aarthi
Thanks a lot for your help. I found the solution :-)
Angeline Aarthi