我有两个xml文件,缺陷。 xml和employee.xml。但是这些文件有一个共同的字段,但每个文件中都有不同的名称。我希望将这两个文件合并到一个数组集合中。
我的defect.xml文件的结构是:
<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>
我的employee.xml
<Employees>
<employee>
<employeeId>256148</employeeId>
<employeeName>Angeline</employeeName>
</employee>
<employee>
<employeeId>256158</employeeId>
<employeeName>Aarthi</employeeName>
</employee>
<Employees>
我在两个数组集合中获取了两个xml文件:
<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}"/>
当“assignedTo”匹配“employeeName”时,我希望将“employeeId”添加到defectList数组集合中。我怎样才能做到这一点? 如何遍历数组集合?
如何检查defectList中的assignedTo字段是否等于employeeList中的employeeName字段?有人指导我..
修改
好的,现在我可以比较两个字段,tasksTo of defectList和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 .
}
}
}
但是如何将employeeId字段添加到该特定数组元素,以便我可以使用defectList作为datagrid n显示员工ID的数据提供者?有人可以帮我弄这个吗。
解
我找到了解决方案。这是代码:
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;
}
}
}
}
现在,如果我在datagrid中将datafield作为“employeeId”并将defectList作为dataprovider,我将获得相应Employee Name的employee id。
答案 0 :(得分:1)
我可以回答部分内容......
通过ArrayCollection迭代很容易......
var defect:Object;
var employee:Object;
for each (defect in defectList)
{
for each (employee in employeeList)
{
// do your check here.
}
}
如果在迭代时挂起数组的内容,则需要注意像ForEach这样的迭代器。你可能想要考虑加载第三个数组。
我不确定你是如何从代码片段中创建的XML对象中获取元素值的,但是Adobe Livedocs
上有很多帮助你或许可以使用这样一个事实:一个对象可以以后期绑定的方式引用它的字段......
var elementContents:String = employee["employeeName"];