开发者

How to count duplicate items in XMLList and assign them into an ArrayCollection?

I've following 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>

I would like eliminate blank node and to make an ArrayCollection like ( with count of individual party),

tArr = new Array开发者_开发百科Collection([ {Party:"A", Count:3}, {Party:"B", Count:2}, {Party:"C", Count:3}, {Party:"D", Count:2}, {Party:"E", Count:1}, ]);

Thanks in advance.


This is untested and may not be the most efficient, but should work:

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);
}


If you have the list of possible parties it's just:

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} )
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜