Skip to main content
Known Participant
May 3, 2013
Answered

How to get Total Number of XML Nodes?

  • May 3, 2013
  • 1 reply
  • 1268 views

Hello All,

I have a Flash program I'm doing in Actionscript 3, using CS6.

I'm using the XMLSocket Class to read-in XML Data. I will write some sample XML Data that is being sent to the Flash

program below...

I know with this line here (below) I can access the 4th "element or node" of that XML Data.

     Accessing XML Nodes/Elements:

// *I created an XML Variable called xml, and "e.data" contains ALL the XML Data
var xml:XML = XML(e.data);

// Accessing the 4th element of the data:

xml.MESSAGE[3].@VAR;          --->     "loggedOutUsers"

xml.MESSAGE[3].@TEXT;         --->     "15"

     SAMPLE XML DATA:


     <FRAME>

0               <MESSAGE VAR="screen2Display" TEXT="FRAME_1"/>

1               <MESSAGE VAR="numUsers" TEXT="27"/>

2               <MESSAGE VAR="loggedInUsers" TEXT="12"/>

3               <MESSAGE VAR="loggedOutUsers" TEXT="15"/>

4               <MESSAGE VAR="admins" TEXT="2"/>

     </FRAME>

I'm new to Flash and Actionscript but I'm very familiar with other languages and how arrays work and such, and I know for

example, in a Shell Script to get the total number of elements in an array called "myArray" I would write something like

this --> ${#myArray[@]}. And since processing the XML Data looks an awful lot like an array I figured there was maybe

some way of accessing the total number of "elements/nodes" in the XML Data...?

Any thoughts would be much appreciated!

Thanks in Advance,

Matt

This topic has been closed for replies.
Correct answer vamsibalu

Hey vamsibatu, thanks again for the quick reply!

Ohhh, ok I gotcha. That makes more sense.

So I just tried this loop below and I guess I could use this and just keep assigning an int variable to the output so

when it finishes I will be left with a variable containing the total number of elements:

for (var x:int in xml.MESSAGE)
{
     trace("x == " + x);
}


*Which OUTPUTS the Following:

x == 0

x == 1

x == 2

x == 3

x == 4

So I guess I could do something like this and when the loop completes I will be left with the total number of elements/nodes...

var myTotal:int;

for (var x:int in xml.MESSAGE)

{

    myTotal = x;

}

// add '1' to myTotal since the XML Data is zero-based:

myTotal += 1;

trace("myTotal == " + myTotal);

*Which Prints:

"myTotal == 5"

Thanks again for you suggestions, much appreciated!

I think that should be good enough for what I needed. Thanks...

Thanks Again,

Matt


even you no need to run for loop to know the length

var levelList:XMLList = xml.children();

levelList.length(); will give you the length of all childrens in your case MESSAGE nodes length;

Pls click helpfull if my answer is helpfull.

Thanks,

Bala

1 reply

Participating Frequently
May 3, 2013

xml.childrens();

or

var xmllst:XMLList = xml.MESSAGE

trace("length=",xmllst.length)

You can try.

Thanks,

Bala

mrm5102Author
Known Participant
May 3, 2013

Hey vamsibatu, thanks for the reply!

vamsibalu wrote:

xml.childrens();

or

var xmllst:XMLList = xml.MESSAGE

trace("length=",xmllst.length)

You can try.

Thanks,

Bala

Not sure if xml.childrens(); was a typo or not but that gives and error. Did you mean to say "xml.children();"..?

Also, I'm using the XMLSocket Class, not sure the differences between XMLList and XMLSocket but when I try to

use the "xml.length()" line it only returns a "1"... Maybe because its enclosed in the <FRAME>...</FRAME> tags??

Thanks Again for the Reply..!!

Thanks,

Matt

Participating Frequently
May 3, 2013

xml.MESSAGE[0] will give first MESSAGE node, but if you don't specify index and write only

xml.MESSAGE itwill return you xmllist contains all MESSAGE nodes.

xmllist work like array...