views:

82

answers:

2

I retrieve XML data in Flex via HttpService. I have the resultFormat property on the HttpService instance set to HTTPService.RESULT_FORMAT_OBJECT. The result contains data similar to this:

<!-- ... -->
<children>
  <item><!-- ... --></item>
  <item><!-- ... --></item>
  <!-- ... -->
<children>
<!-- ... -->

I get an array named item beneath the element children when there is more than one item sibling. If there's only one, the conversion can't distinguish it from a scalar.

What do I need to change to have Flex convert all item elements to an array with 0 or more elements?

What would I have to do to get an array member children, dropping the item wrappers altogether?

The XML is generated by Struts on the server side. I always have the option to change the structure of the document there, but right now I'm interested in what I can do with Flex.

A: 

The item object should be an XMLList whether there is one entry or several. (It will be undefined if there are none.)

You can use XML methods on it if there is only one element, but that's just as a convenience. See the XMLList documentation.

I also recommend comment 5 on this blog entry by Mike Morearty (cached version as the site appears to be inaccessible).

Michael Brewer-Davis
That link is nice. So, the basic takeaway then is to use e4x results, right?
Hanno Fietz
A: 

Try this:

try {
    for (var i:int = 0; /* loop forever till error is thrown */; i++) {
        var j:String = String( children.item[ i ].SomeOtherAttribute ); // if its a string
    }
} catch (re:RangeError) {
}
sri
I don't get it. What's that code supposed to do? Also, allow me to frown on what seems a slight abuse of exceptions. (If I can do item[i], sure there's item.length to be used for a loop condition, no?)
Hanno Fietz
sri