Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to get Total Number of XML Nodes?

Explorer ,
May 03, 2013 May 03, 2013

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

TOPICS
ActionScript
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , May 03, 2013 May 03, 2013

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

Translate
Explorer ,
May 03, 2013 May 03, 2013

xml.childrens();

or

var xmllst:XMLList = xml.MESSAGE

trace("length=",xmllst.length)

You can try.

Thanks,

Bala

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 03, 2013 May 03, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 03, 2013 May 03, 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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 03, 2013 May 03, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 03, 2013 May 03, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 03, 2013 May 03, 2013
LATEST

Ohhh, I see.

At first I didn't see that when you created the XMLList variable you were actually assigning it to MY VARIABLE "xml" with

the ".children()"... I just thought you were showing another way I could go instead of using XMLSocket.... But I gotcha now!!

Much appreciated, thanks for the explainations!

Thanks AGAIN,

Matt

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines