[英]How do I make Flex create one-element arrays from XML data?
I retrieve XML data in Flex via HttpService. 我通过HttpService在Flex中检索XML数据。 I have the
resultFormat
property on the HttpService instance set to HTTPService.RESULT_FORMAT_OBJECT
. 我在HttpService实例上将
resultFormat
属性设置为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. 当有多个同级
item
时,我在子元素下得到一个名为item的数组。 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? 要让Flex将所有
item
元素转换为包含0个或多个元素的数组,我需要更改什么?
What would I have to do to get an array member children
, dropping the item
wrappers altogether? 我要怎么做才能让一个数组成员
children
,完全丢弃item
包装器?
The XML is generated by Struts on the server side. XML由服务器端的Struts生成。 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.
我总是可以选择在那里更改文档的结构,但是现在我对Flex可以做什么感兴趣。
The item
object should be an XMLList
whether there is one entry or several. 无论有一个条目还是多个条目,
item
对象都应该是一个XMLList
。 (It will be undefined
if there are none.) (如果没有,则将是
undefined
。)
You can use XML
methods on it if there is only one element, but that's just as a convenience. 如果只有一个元素,则可以在其上使用
XML
方法,但这只是为了方便。 See the XMLList documentation . 请参阅XMLList文档 。
I also recommend comment 5 on this blog entry by Mike Morearty (cached version as the site appears to be inaccessible). 我还建议Mike Morearty在此博客条目上添加评论5(由于该网站似乎无法访问,因此为缓存版本)。
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) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.