将XML绑定到Flex树的问题

时间:2022-01-09 04:53:10

I have XML with a value like the following:

我有一个XML,其值如下:

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

我通过调用xml.children()来声明XMLList,并将其绑定到树,如下所示:

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:

但是,我的树不知道要为哪些XML元素创建节点,因此我为每个元素获取节点,即:

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

我真正想要的只是显示每个 的@name值:

  • All Products
    • Product Group A
      • Product A
    • 产品组A产品A.

  • 所有产品产品组A产品A.

Am I missing something totally obvious?

我错过了一些完全明显的东西吗

1 个解决方案

#1


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

如果您只是从树中收集节点,我认为E4X表示法是最好的方法:

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:

但是,这不会保留树结构。由于您需要树数据的特定视图,我建议应用ITreeDataDescriptor的实现,该实现在其方法中过滤命名节点:

#1


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

如果您只是从树中收集节点,我认为E4X表示法是最好的方法:

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:

但是,这不会保留树结构。由于您需要树数据的特定视图,我建议应用ITreeDataDescriptor的实现,该实现在其方法中过滤命名节点: