[英]ActionScript/Flex: build an XML structure from arrays
I am building an application which works on data from column based database. 我正在构建一个应用程序,该应用程序可以处理基于列的数据库中的数据。 I am getting the data as arrays where each array represents a columns in the table returned by the database.
我将数据作为数组获取,其中每个数组代表数据库返回的表中的列。 The data contain parent, child relationship.
数据包含父子关系。 The arrays may look like that (view same index in each array as a row in a normal SQL database):
数组看起来像这样(在普通SQL数据库中,每个数组的索引与行中的索引相同):
[0] empty [1] empty [2] ID-A [3] ID-B [4] ID-B <-- this represents nodes' parents
[0] ID-A [1] ID-A [2] ID-B [3] ID-C [4] ID-D <-- this represents nodes' labels
[0] 100 [1] 200 [3] 300 [4] 150 [4] 150 <-- this represents values associated with nodes
Of course they are much bigger. 当然,它们更大。 Up to 100000 elements.
最多100000个元素。 All I want to do, is create the following XML from the data I have:
我要做的就是根据我拥有的数据创建以下XML:
<root>
<node label="ID-A" value="300">
<node label="ID-B" value="300">
<node label="ID-C" value="150"/>
<node label="ID-D" value="150"/>
</node>
</node>
</root>
Notice that for ID-A there is only one entry at the top level, and it's value is the sum for all its entries in the array. 请注意,对于ID-A,顶层只有一个条目,它的值是数组中所有条目的总和。
I do not know the depth of the structure beforehand and any IDs or values. 我事先不知道结构的深度以及任何ID或值。 How do I create the XML so that I will be later able to display it in a tree control?
如何创建XML,以便以后可以在树控件中显示它? What I want to do is iterate through array, each time adding a node for an ID if it does not exist, but just update the value (add it to the current one) if the ID exists.
我想做的是遍历数组,每次为ID添加一个节点(如果它不存在),但是如果ID存在,只需更新值(将其添加到当前节点)。 I can create the first layer, but I have trouble accessing and updating appropriate elements at bigger depths.
我可以创建第一层,但是在更大深度访问和更新适当的元素时遇到了麻烦。 Basically, how do would I add a ID-E below ID-B to the XML above, in order to achieve the following:
基本上,我将如何在ID-B下添加一个ID-E到上面的XML中,以实现以下目的:
<root>
<node label="ID-A" value="300">
<node label="ID-B" value="300">
<node label="ID-C" value="150"/>
<node label="ID-D" value="150"/>
<node label="ID-E" value="sth"/>
</node>
</node>
</root>
Or would it maybe actually be beter to build an ArrayCollection with children from the arrays I have? 还是用从我拥有的阵列中获得的子代构建ArrayCollection可能实际上更好? I am new to Flex so I can't tell myself what would be more efficient.
我是Flex的新手,所以我不能告诉自己什么会更有效。
Converting 100k rows into XML could be very slow and XML takes up a lot of memory, which is wasted if it's just for use as a tree data provider. 将10万行转换为XML可能非常慢,并且XML占用大量内存,如果仅用作树数据提供者,则将浪费资源。 If you just need to do that to view the data in a tree, perhaps you can keep it in an Array, but use a customer tree dataDescriptor to define the hierarchy.
如果只需要这样做就可以查看树中的数据,也许可以将其保留在Array中,但是可以使用客户树的dataDescriptor定义层次结构。 http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/Tree.html#dataDescriptor
http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/mx/controls/Tree.html#dataDescriptor
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.