如何计算XMLList中的重复项并将它们分配到ArrayCollection中?

时间:2010-05-19 08:51:58

标签: flex

我遵循XMLList,

<party/> <party/> <party/> <party>A</party> <party>B</party> <party>C</party> <party>A</party> <party>B</party> <party>C</party> <party>D</party> <party>E</party> <party>D</party> <party>A</party> <party/> <party>C</party>

我想消除空白节点并使ArrayCollection像(个别方的数量),

tArr = new ArrayCollection([{Party:“A”,Count:3},                       {派对:“B”,伯爵:2},                       {党:“C”,伯爵:3},                   {派对:“D”,伯爵:2},                   {派对:“E”,伯爵:1},                      ]);

提前致谢。

2 个答案:

答案 0 :(得分:0)

这是未经测试的,可能不是最有效的,但应该有效:

var partyDict:Dictionary = new Dictionary();
var parties:ArrayCollection = new ArrayCollection();

var xml:XML = <root><party/><party/><party/><party>A</party><party>B</party><party>C</party><party>A</party><party>B</party><party>C</party><party>D</party><party>E</party><party>D</party><party>A</party><party/><party>C</party></root>;

for each (var p:XML in xml.party) {
    var val:String = p.toString();
    if ((val != null) && StringUtil.trim(val).length > 0) {
        if (partyDict[val] != null) {
            partyDict[val] = (partyDict[val] as int) + 1; // may simply be able to do partyDict[val]++;
        } else {
            partyDict[val] = 1;
        }
    }
}

for (var key:Object in partyDict) {
    var o:Object = new Object();
    o.Party = key;
    o.Count = partyDict[key];
    parties.addItem(o);
}

答案 1 :(得分:0)

如果您有可能派对的列表,那就是:

var partiesObjs:ArrayCollection = new ArrayCollection();

var xml:XML = <root><party/><party/><party/><party>A</party><party>B</party><party>C</party><party>A</party><party>B</party><party>C</party><party>D</party><party>E</party><party>D</party><party>A</party><party/><party>C</party></root>;
var parties:Array = ["A","B","C","D"]

for each(var p:String in parties){
  var count:int = xml..party.(toString() == p).length()
  partiesObjs.addItem( {Party:p, Count:count} )
}