0

在 HTTPService 调用的 resultHandler 中,我已将重复节点分配给 arrayCollection。在该重复节点内是有时重复有时不重复的其他节点。例如,这里的option节点在options内重复。

<response> 
   <options>
      <option> <var1> part1 </var1> <var2> part2 </var2> </option>
      <option> <var1> part1 </var1> <var2> part2 </var2> </option>
   </options>
   <options>....
</response>

有时它不会重复,就像这样。

 <response> 
       <options>
          <option> <var1> part1 </var1> <var2> part2 </var2> </option>
       </options>
       <options>....
 </response>

我在我的 for 循环中遇到了 actionscript 错误。我如何解释这两种情况?

这是我的 for 循环将对象分配给值对象:

protected function xml_resultHandler(event:ResultEvent):void
  {
  var data:ArrayCollection = xml.lastResult.response.option;
  var valueobjects:valueObject;

  for each (var characteristic:Object in data)
   {
                        valueobject = new valueobject;
                        valueobject.var1 = characteristic.option[0].var1;
                        valueobject.var2 = characteristic.option.var2;
                        datamodel.addItem(valueobject);
                    }

}

在此示例代码中,如果没有多个选项节点,分配 var1 将中断,如果有多个对象节点,分配 var2 将中断。我可以将它分开并分别迭代它们,但有没有更雄辩的解决方案?

4

1 回答 1

0

这样可以吗

protected function xml_resultHandler(event:ResultEvent):void
{
    var data:ArrayCollection = xml.lastResult.response.option;
    var valueobjects:valueObject;

    for each (var characteristic:Object in data)
    {
                    valueobject = new valueobject;
                    if(characteristic.option is ArrayCollection)
                        valueobject.var1 = characteristic.option[0].var1;
                    else
                        valueobject.var2 = characteristic.option.var2;
                    datamodel.addItem(valueobject);
    }
}

I've used something very similar to this for result handlers where I'm not sure if the data will contain 1 or multiple rows and it seems to do the trick, seems when using the dot operator for E4X parsing it will either return an object or an arraycollection depending on the multiplicity.

于 2011-01-27T22:41:54.053 回答