title: ActionScript/Flex: build an XML structure from arrays
body: <p>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):  </p>
  
  <pre><code>[0] empty   [1] empty   [2] ID-A  [3] ID-B  [4] ID-B &lt;-- this represents nodes' parents  
  [0] ID-A    [1] ID-A    [2] ID-B  [3] ID-C  [4] ID-D &lt;-- this represents nodes' labels  
  [0] 100     [1] 200     [3] 300   [4] 150   [4] 150  &lt;-- this represents values associated with nodes
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>&lt;root&gt;  
     &lt;node label="ID-A" value="300"&gt;  
        &lt;node label="ID-B" value="300"&gt;  
            &lt;node label="ID-C" value="150"/&gt;  
            &lt;node label="ID-D" value="150"/&gt;  
        &lt;/node&gt;
     &lt;/node&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>&lt;root&gt;  
     &lt;node label="ID-A" value="300"&gt;  
        &lt;node label="ID-B" value="300"&gt;  
            &lt;node label="ID-C" value="150"/&gt;  
            &lt;node label="ID-D" value="150"/&gt;  
            &lt;node label="ID-E" value="sth"/&gt;
        &lt;/node&gt;
     &lt;/node&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>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.</p>