How do I make Flex create one-element arrays from XML data?
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 ge开发者_开发技巧nerated 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.
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).
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) {
}
精彩评论