Count Total XML Nodes In Flash

Hello Experts,

I'm using an XML file to create a tree menu in Flash. I need to somehow obtain the total nodes from my XML documents. The XML file looks like this:

<node>
      <node label="Flashpaq Setup">
            <node label="About" />
            <node label="Set Contrast" />
            <node label="Set Backlight" />
      </node>
      <node label="Performance Tuning">
            <node label="Read Vehicle">
                  <node label="Read Vehicle ID" />
                  <node label="Read Stock" />
            </node>
            <node label="Change Setup">
                  <node label="Power Level">
                        <node label="Performance" />
                        <node label="Tow/Perf" />
                        <node label="Tow" />
                        <node label="Stock" />
                  </node>
                  <node label="Options">
                        <node label="Speed Limiter" />
                        <node label="Tire Height" />
                  </node>
            </node>
            <node label="View Settings">
                  <node label="Flashpaq Status" />
                  <node label="Power Level" />
                  <node label="Options" />
            </node>
            <node label="Program Vehicle" />
      </node>
      <node label="Diagnostics">
            <node label="Read OTC's"/>
            <node label="Clear OTC's" />
            <node label="Data Logging" />
      </node>
</node>

There are a total of 26 nodes without counting root. A node begins like this "<node..."

Pretty Urgent
350 PTS
Thanks,

Vic
vdiaz761Asked:
Who is Participating?
 
Ramy_atefConnect With a Mentor Commented:
HI
I've made a file for u

www.pylon-group.com/temp/xml.zip

regards,
Ramy
0
 
Aneesh ChopraConnect With a Mentor Commented:
Hi,

1.
open a new FLA and paste the following code on first frame:
-------------
var count = 0;
var myXML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = function(success)
{
      if (success)
      {
            node = myXML.firstChild;
            countNodes(node);
            trace("total node count is : "+count);
      }
};
myXML.load('myXML.xml');
countNodes = function (xmlnode)
{
      for (var i = 0; i<xmlnode.childNodes.length; i++)
      {
            count++;
            if (xmlnode.childNodes[i].hasChildNodes)
            {
                  countNodes(xmlnode.childNodes[i]);
            }
      }
};
-----------

2.
copy your xml in the same folder where your fla is, and I assume xml name as "myXML.xml"



Rgds
Aneesh
0
 
wings_gauravCommented:
function countChildNodes(objNode:XMLNode)
{
      var count = 0;
      for(var i =0; i < objNode.childNodes.length; i++)
      {
            count += countChildNodes(objNode.childNodes[i]);
      }
      return count + objNode.childNodes.length;
}

var objXML:XML = new XML();
objXML.onLoad = function(sucess)
{
      if(sucess)
      {
            trace(countChildNodes(objXML.firstChild)); // trace the count
      }
}
objXML.ignoreWhite = true;
objXML.load("test.xml");

-wings
0
 
wings_gauravCommented:
sorry, i guess anish had already posted the answer!

-wings
0
 
vdiaz761Author Commented:
Thanks Guys...

Reasoning behind point distribution:

Aneesh produced working solution 1st. 225pts
Ramy produced working solution 2nd. 125pts

Thank all three of you for the quick response.

Vic
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.