【问题标题】:Update array nodes using associative array使用关联数组更新数组节点
【发布时间】:2014-05-01 06:09:13
【问题描述】:

我有一个像这样的数组:

> ObjectArr[ID,x,y,distance,Type];

还有一个类似的xml:

 objectXML = 
    <objects>
      <object id={ID} x={x} y={y} distance={distance}>{Type}</object>
      <object id={ID} x={x} y={y} distance={distance}>{Type}</object>
      <object id={ID} x={x} y={y} distance={distance}>{Type}</object>
      <object id={ID} x={x} y={y} distance={distance}>{Type}</object>
    </objects>

目标:根据objectArr更新XML节点

objectArr.length = 4 and objectXMl.length() = 4

我是这样做的:

for(var i:int = 0; i < objectXML.length(); i++)
{
  objectXML.object[i].@ID = objectArr[i].ID;
  objectXML.object[i].@x = objectArr[i].x;
  objectXML.object[i].@y = objectArr[i].y;
  objectXML.object[i].@distance = objectArr[i].distance;
  objectXML.object[i]= objectArr[i].Type;
}

但是 xml 没有根据数组元素进行更新。 我应该选择其他解决方案吗??

如果我需要在删除后更新 xml,我该怎么办?? 为了删除数组我写了这么多

var ID:String = Obj.getObjectId();
for(var i:int=0; i < objectArr.length; i++)
{
  if(objectArr[i].objID == ID)
  objectArr.splice(i, 1);
}

为了删除 xml 节点,我编写了这段代码 ::

for(var i:int = 0; i<objectXML.object.length();i++)
{
  if(objectXML.object.length() && objectXML.object[i].@objID == objectArr[i].objID)
     delete objectXML.object[i];
}

但它显示了这个错误

错误 #1010:术语未定义且没有属性。

【问题讨论】:

    标签: arrays xml actionscript-3 apache-flex flex4.5


    【解决方案1】:
    • objectXML.length() 等于 1,而不是 4
    • @ID 应为小写以匹配 xml 中的 id 属性

    试试这个:

    for(var i:int = 0; i < objectXML.object.length(); i++){
       objectXML.object[i].@id=objectArr[i].ID;
       //....
    }
    

    【讨论】:

    • 谢谢你......只是一个愚蠢的错误,但花了我很多时间......我只是忘了检查长度......
    • 很高兴知道它有帮助:)
    • 我已经编辑了这个问题,所以我取消选择接受的标记......所以在我得到答案后,我会选择你的答案作为正确的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 2012-09-20
    • 1970-01-01
    • 2014-09-29
    • 2021-12-09
    • 2020-03-21
    • 2017-03-12
    相关资源
    最近更新 更多