?
Solved

Count Total XML Nodes In Flash

Posted on 2006-05-30
5
Medium Priority
?
251 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:vdiaz761
5 Comments
 
LVL 34

Assisted Solution

by:Aneesh Chopra
Aneesh Chopra earned 900 total points
ID: 16790670
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
 
LVL 5

Expert Comment

by:wings_gaurav
ID: 16790685
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
 
LVL 5

Expert Comment

by:wings_gaurav
ID: 16790698
sorry, i guess anish had already posted the answer!

-wings
0
 
LVL 7

Accepted Solution

by:
Ramy_atef earned 500 total points
ID: 16790746
HI
I've made a file for u

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

regards,
Ramy
0
 

Author Comment

by:vdiaz761
ID: 16791043
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

749 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