Apache – Problem binding XML to a Flex Tree

apache-flex

I have XML with a value like the following:

<products>
  <product id="1" name="All Products">
    <code>000</code>
    <shortname>ALL</shortname>
    <cost>0.0</cost>
    <product id="2" name="Product Group A">
      <code>001</code>
      <shortname>A</shortname>
      <cost>0.0</cost>
      <product id="4" name="Product A">
        <code>11</code>
        <shortname>ProductA</shortname>
        <cost>0.4</cost>
      </product>
    </product>
  </product>
</products>

I declare an XMLList by calling xml.children(), and bind it to a tree like so:

var products:XMLList = xml.children()

<mx:Tree id="treeProducts" dataProvider="{products}" labelField="@name" width="100%" />

However, my tree doesn't know which XML elements to create nodes for, so I get nodes for every element, ie:

-All Products
  - 000
  - ALL
  - 0.0
  - Product Group A
    - 001
    - A
    - 0

What I really want is just to show the @name value for each <product>:

  • All Products
    • Product Group A
      • Product A

Am I missing something totally obvious?

Best Answer

If you were just collecting nodes from the tree, I would think E4X notation would be the best way to go:

xml..(@name != '')

(or some such, not checked for accuracy)

That wouldn't preserve the tree structure, though. Since you want a particular view of the tree data, I'd suggest applying an implementation of ITreeDataDescriptor which filters for named nodes in its methods:

Related Topic