简体   繁体   中英

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):

[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. All I want to do, is create the following XML from the data I have:

<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.

I do not know the depth of the structure beforehand and any IDs or values. How do I create the XML so that I will be later able to display it in a tree control? 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. 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:

<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? I am new to Flex so I can't tell myself what would be more efficient.

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. 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. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/Tree.html#dataDescriptor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM