简体   繁体   English

如何使Flex从XML数据创建一个元素数组?

[英]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.

相关问题 使用 XSLT 1.0 在单元素 JSON 中包装 XML 文档 - Wrapping XML document in one-element JSON using XSLT 1.0 如何在Flex饼图中显示分组的XML数据? - How do I display grouped XML data in a Flex pie chart? (Flex 3)如何使用HTTPservice从xml文件中获取数据并将返回数据另存为数组? - (Flex 3) How do I get data from a xml file using HTTPservice and save the return data as an array? 如何从xml反序列化数据,如何通过xml元素名称动态创建类? - How can I deserialize data from xml and how can I create classes dynamically by xml element name? 如何在Flex / Actionscript中从XML检索节点名称 - How do I retrieve the node name from XML in Flex / Actionscript 如何在PHP中从XML数据创建变量? - How do I create variables from XML data in PHP? 如何从XML文件的解析数据输出创建单个XML文件? - How do I create individual XML files from the parsed data output of a XML file? 如何在Flex 3中将数据从xml获取到DataGrid - How to get the data from xml to a datagrid in flex 3 如何一次处理一个父元素的XML文档? - How do I manipulate an XML document one parent element at a time? Jaxb - 我如何将一个 xml 元素解组到多个字段? - Jaxb - how do i unmarshal one xml element to multiple fields?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM