7 Replies Latest reply on Feb 1, 2008 8:04 PM by ntsiii

    Binding XML to ArrayCollection- Ultimate solution?!

    2JZ Level 1
      All,

      I am trying to get a full understanding of access to ArrayCollection here. How do I build a perfect binding procedure?

      Let say my XML file is like as follow:
      <?xml version="1.0" encoding="utf-8"?>
      <folders>
      <folder label="ParentTag1">
      <folder label="ChildTag1"/>
      <folder label="ChildTag2">
      <folder label="ChildTag3">
      <file item="Photo1"/>
      <file item="Photo2"/>
      <file item="Photo3"/>
      </folder>
      </folder>
      </folder>
      </folders>

      MyAC = new ArrayCollection();
      if( rpcEvent.result.Folder.ParentTag.ChildTag is ArrayCollection )
      A) MyAC = rpcEvent.result.Folder.ParentTag.ChildTag as ArrayCollection;

      B) MyAC.addItem( rpcEvent.result.Folder.ParentTag.ChildTag );

      C) MyAC = new ArrayCollection( ArrayUtil.toArray(rpcEvent.result.Folder.ParentTag.ChildTag ) );

      Solution A works when the result is ArrayCollection type.
      Solution B works when the result is a single object.
      Solution C works when the result is not ArrayCollection type but can be casted to ArrayCollection (ex: more than one object).

      I have this in mind but, I do not think that's appropiate.

      if( rpcEvent.result.Folder.ParentTag.ChildTag is ArrayCollection )
      MyAC = rpcEvent.result.Folder.ParentTag.ChildTag as ArrayCollection;
      else
      MyAC = new ArrayCollection( ArrayUtil.toArray(rpcEvent.result.Folder.ParentTag.ChildTag ) );
      if( MyAC.length == 0 )
      MyAC.addItem( rpcEvent.result.Folder.ParentTag.ChildTag );

      What is the correct way to load the XML to the ArrayCollection?

      to get the label of the ChildTags, I use the following:

      ChildLabel = MyAC[0].label;

      But, it crashes when the size of MyAC is 1. What is the correct way to retrieve the first data from ArrayCollection? I want to get a piece of code that works for all cases.