[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

count all nodes in XML?

Posted on 2006-05-18
5
Medium Priority
?
294 Views
Last Modified: 2013-11-19
I would like to count all the nodes in xml....If I have a XML like this.,...

<category name="a">
       <item id="1">
       <item id="2">
       <item id="3">
</category>
<category name="b">
       <item id="4">
       <item id="5">
       <item id="6">
</category>

I should get 8...or if its not possible...Is there a way I can able to count all the nodes which has the name="item" ...is it possible?

Thanks


My code just gives me the final three...but I want all the items starts with the node name called ITem or...I should able to all subItems.

import mx.xpath.XPathAPI;
var rssfeed_xml = new XML();
rssfeed_xml.ignoreWhite = true;
rssfeed_xml.load("bts_navigation.xml");
rssfeed_xml.onLoad = function(success)
{
      if (success)
      {
        parseStart(this);
    }
       else
      {
       trace("error loading XML");
    }
};
function parseStart(xmlData)
{
      var MenuItem:Object= new Object();
      mainNode = xmlData.firstChild.childNodes;
      for (z=0;z<mainNode.length;z++){
            //trace(mainNode[z].childNodes);
            itemNode = mainNode[z].childNodes
            for(i=0;i<itemNode.length;i++){
                  MenuItem.ID = itemNode[i].attributes.id;
                  MenuItem.CatLabel = "";
                  MenuItem.ButtonLabel = itemNode[i].attributes.label;
                  MenuItem.PMMSID = itemNode[i].attributes.pmmsid;
                  MenuItem.DL = itemNode[i].attributes.dl;
                  MenuItem.PN = itemNode[i].attributes.pn;
                  MenuItem.MU = itemNode[i].attributes.mu;
                  MenuItem.CID = itemNode[i].attributes.cId;
                  trace(MenuItem.length+"   MenuItem.length");
                  trace(MenuItem.ID+"  MenuItem.ID");
                  trace(MenuItem.PMMSID+"  MenuItem.pmms");
                  trace(MenuItem.MU+"  MenuItem.mu");
                  trace(MenuItem.CID+"  MenuItem.CID");
            }
            
      }
}



0
Comment
Question by:SubhaBabu
  • 2
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
Montoya earned 500 total points
ID: 16710694
Hey there.. I believe your XML is not well formed. I would expect this:

*******XML*************

<?xml version="1.0" encoding="UTF-8"?>
<Categories>
<category name="a">
       <item id="1"/>
       <item id="2"/>
       <item id="3"/>
</category>
<category name="b">
       <item id="4"/>
       <item id="5"/>
       <item id="6"/>
</category>
</Categories>

*******XML*************



Now, with that said, you could do this:

var categories_xml:XML = new XML();           //create XML Object
categories_xml.ignoreWhite = true;               // you know, ignore white spaces
categories_xml.onLoad = function (success:Boolean):Void {    //what to do onLoad of file
    if (success) {  //  If you succeed...
       myArray=[];    //create an empty array. Right way would be var myArray:Array = new Array();
            for(var i=0;i<categories_xml.firstChild.childNodes.length;i++){                      //for every childNode (Category) for my firstChild(Categories)
                  
                                    
                  for(var j=0;j<categories_xml.firstChild.childNodes[i].childNodes.length;j++){   //go through each childNode (Category) for my firstChild(Categories) and count the length
                        
                        myArray.push(categories_xml.firstChild.childNodes[i].childNodes[j]);  //push each "item" into my array.

                        }

            }
                                       trace(myArray.length);  //trace the length of myArray when its all done.
            
    } else {
        trace("Unable to load external file.");
    }
}


categories_xml.load("categories.xml");

0
 
LVL 34

Assisted Solution

by:Aneesh Chopra
Aneesh Chopra earned 500 total points
ID: 16710780
Hi,

xml structure also has problem,
xml should have only one top level node
here is the fixed xml
-------------
<?xml version="1.0"?>
<root>
      <category name="a">
            <item id="1"/>
            <item id="2"/>
            <item id="3"/>
      </category>
      <category name="b">
            <item id="4"/>
            <item id="5"/>
            <item id="6"/>
      </category>
</root>
------------

here is the updated code to read this XML nodes:
========
var rssfeed_xml = new XML();
rssfeed_xml.ignoreWhite = true;
rssfeed_xml.load("bts_navigation.xml");
rssfeed_xml.onLoad = function(success)
{
      if (success)
      {
            parseStart(this);
      } else
      {
            trace("error loading XML");
      }
};
function parseStart(xmlData)
{
      var MenuItem:Object = new Object();
      mainNode = xmlData.firstChild.childNodes;
      for (z=0; z<mainNode.length; z++)
      {
            trace(mainNode[z].nodeName);
            itemNode = mainNode[z].childNodes;
            for (i=0; i<itemNode.length; i++)
            {
                  trace("MenuItem.name "+itemNode[i].nodeName);
                  trace("MenuItem.ID  "+itemNode[i].attributes.id);
            }
      }
}
============

Rgds
Aneesh
0
 
LVL 19

Expert Comment

by:Montoya
ID: 16710889
that's funny. Looks like we posted moments apart.

~Montoya
0
 
LVL 34

Expert Comment

by:Aneesh Chopra
ID: 16714932
I just noticed it :)
0
 
LVL 2

Author Comment

by:SubhaBabu
ID: 16717351
Thanks guys...I already have correct version of xml with one root element...but...the loop code really helps....Thanks Aneesh & Montoya
0

Featured Post

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!

Question has a verified solution.

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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
Suggested Courses

873 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