Solved

ActionScript - Count matching children in XML

Posted on 2010-11-30
6
702 Views
Last Modified: 2012-05-10
I have the XML below come back from a server and put it into _historicalAlarms which is an XML object.

I am wanting to build a pie chart that shows % red alerts vs yellow alerts

I build out my Pie chart XML by calling this function for each distinct Alarm name:

                  private function getNameCount(name:String):XML {
                        var nameCount:int = _historicalAlarms.Alarm.(Name == name).length();
                        trace("Name count for " + name + " = "  + nameCount);
                        return <NameCount name={name} count={nameCount}/>
                  }

nameCount is always 0.  Can anyone help me diagnose why this is not returning a true count.  In the example XML it would send the name "Red Alert" and should get 3 back for a count.
<Results>
  <Alarm id="2266">
    <Name>Red Alert</Name>
  </Alarm>
  <Alarm id="2286">
    <Name>Red Alert</Name>
  </Alarm>
  <Alarm id="2306">
    <Name>Yellow Alert</Name>
  </Alarm>
  <Alarm id="2486">
    <Name>Red Alert</Name>
  </Alarm>
</Results>

Open in new window

0
Comment
Question by:trippy1976
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
Gvoid earned 250 total points
ID: 34242065
You can't do it like that, it's a good try.

whenever you call
var nameCount:int = _historicalAlarms.Alarm.(Name == name).length();

the code will not know which 'Alarm' node to use, also there is only ever one Name node embeded, so you will only ever get back 0 from something like this.

you need to itterate through the list and count the occurances of each item you are interested in.

e.g.
count 

private function getNameCount():void {
 var redCount : int = 0;
 var yellowCount : int = 0;
 for (var i in _historicalAlarms.Alarm) {
   if (_historicalAlarms.Alarm[i].Name == "Red Alert"){
     redCount++;
   } else {
     yellowCount++;
   }
  }
  trace ("redCount: " + redCount);
  trace ("yellowCount: " + yellowCount);
}

Open in new window

0
 
LVL 4

Author Comment

by:trippy1976
ID: 34242973
The example was probably too convenient, but I won't really know what's coming in that list.  I can see how to adapt what you have, but I'd rather do it with some more "elegant" finder if that's feasible.

If I do just

_historicalAlarms.Alarm.length()

I actually get the right count of total alarms.
0
 
LVL 1

Expert Comment

by:Gvoid
ID: 34245855
oh right, sorry I miss understood try this

var names : XMLList = historicalAlarms.descendants("Name");
trace(names.length())
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Assisted Solution

by:tomaugerdotcom
tomaugerdotcom earned 250 total points
ID: 34248269
Use an associative array to count the occurrences of like Names
var alertsXML:XML = 
	<Results>
	  <Alarm id="2266">
		<Name>Red Alert</Name>
	  </Alarm>
	  <Alarm id="2286">
		<Name>Red Alert</Name>
	  </Alarm>
	  <Alarm id="2306">
		<Name>Yellow Alert</Name>
	  </Alarm>
	  <Alarm id="2486">
		<Name>Red Alert</Name>
	  </Alarm>
	</Results>;
	

// create an associative array to hold the various alarm types
var alarmsCount:Object = new Object();

// iterate through the XML
for each (var alarm:XML in alertsXML.Alarm){
	// increment the corresponding associative array key
	alarmsCount[alarm.Name] = alarmsCount[alarm.Name] ? alarmsCount[alarm.Name] + 1 : 1;
}

// now, list em out
for (var alarmType:String in alarmsCount){
	trace(alarmType + ":" + alarmsCount[alarmType]);
}

Open in new window

0
 
LVL 4

Author Closing Comment

by:trippy1976
ID: 34248482
I believe either way will work.  I hoped there was a more elegant way to do it similar to xpath, but based on two replies I and failure to find it myself I'm guessing that this is the way.  You have to parse through the XML one way or another and keep track.

Thanks guys.

FWIW - here is what I did...

- Put the XML into a string variable
- Split the string on the name I was looking for with the element text around it
- Took the count as array.length() - 1

This is also brute, but it works.
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 34248546
The challenge here was that you didn't want to hard-code the names. We could have done something that looked more elegant if we knew the Names beforehand (Alarm.(Name=="Red Alert").length()); but that didn't meet your requirements. Not sure how XPath would have been any more use in this scenario.

-- T
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question