1 Reply Latest reply on Oct 5, 2006 4:01 PM by Ham0

    XML to Array Collections

    nated
      I've recently come across an issue with converting XML to an ArrayCollection. If my XML has more than one "item" in the collection and the XML is something like this:
      <base>
      <items>
      <item>
      <title>one</title>
      </item>
      <item>
      <title>two</title>
      </item>
      </items>
      </base>

      All I have to do in my AS is var ac:ArrayCollection = httpService.lastResult.base.items.item;

      However, if I only have one item in my xml :
      <base>
      <items>
      <item>
      <title>one</title>
      </item>
      </items>
      </base>

      This won't convert to the ArrayCollection via the AS:
      var ac:ArrayCollection = httpService.lastResult.base.items.item;
      That causes a runtime error. No runtime error if list is > 1 though!

      I converted my code to accomidate a list of length 1 by the AS:
      var newArray:Array = ArrayUtil.toArray(httpService.lastResult.base.items.item);

      If there are more than one items in the list (example 1), then newArray is an array of ArrayCollections with length 1. newArray.pop() will actually pop off an ArrayCollection, which you then have to getItemAt(index) to get an Object

      If there is only one item, then newArray.pop() will pop off an Object

      I don't get it?! Has anyone else run into issues like this?
        • 1. Re: XML to Array Collections
          Ham0
          Just spent time wresteling with this stuff myself. Still getting used to it all but I beieve the problem is that a 1 item list is treated as an "object" not an "array". Since there aren't multiple indexes in the XML, the array wrapper is disregarded.

          Objects can't be assigned directly to an AC, so you have to convert it to an array using toArray or an event function.

          I posted some examples in the FlexBuilder forum that may be useful.