2 Replies Latest reply on Nov 27, 2007 12:50 PM by ntsiii

    XML to model, problem with arrays

      I've imported XML data from the server using the HTTPService and using the service to convert the XML to a model. So below is my XML:

      <field label="" value=""/>
      <field label="" value=""/>

      I have to deal with the situation where if there is only one field, details.field is not Array otherwise it is but the following condition doesn't seem to work:

      if (details.field is Array)

      but I can still iterate through the elements as if it was an array. So if details.field is not an Array, what is it? Or more generally how do I find out the class of a variable?
        • 1. Re: XML to model, problem with arrays
          markh Level 1
          It seems arrays in automatically constructed models are mx.collections.ArrayCollection not Array.
          • 2. Re: XML to model, problem with arrays
            ntsiii Level 3
            The default resultFormat of HTTPService is object. This causes your xml to be converted to a nested object structure. This is rarely desirable. And it causes the issue you are seeing, because the convertor can't differentiate between an array with a single element and an object..

            Instead, set resultFormat="e4x". then, in the result handler, do:
            var xmlResult:XML = XML(event.result);

            The expression, xmlResult.field, will return an XMLList. You can iterate over that using a normal for loop with bracketed index style references.

            There are also solutions to your issue if you want to stay with resultFormat="object". They involve, as you expect, examining the the result object for its contained datatype. You can probably find an example in the archives.

            But switch to XML, and drop Model. Look up e4x xml if you want more erasons.