|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
|
|
#1
|
|||
|
|||
|
I am having trouble getting a node count from an XML data list.
<menu> <catagory> <title>Catagory One for the money</title> <sub1>This is sub1 one</sub1> <sub2>This is sub2 one</sub2> <sub3>This is sub3 one</sub3> </catagory> <catagory> <title>Catagory Two for the show</title> <sub1>This is sub1 two</sub1> <sub2>This is sub2 two</sub2> </catagory> </menu> menu=new ActiveXObject ("MSXML.DOMDocument"); menu.load("XML FileName.xml"); testNode=menu.documentElement.firstChild; document.write(testNode.firstChild.nodeName); this yeilds ---> title document.write(testNode.firstChild.nextSibling.nodeName); this yeilds ---> sub1 document.write(testNode.firstChild.nextSibling.nextSibling.nodeName); this yeilds ---> sub2 document.write(testNode.lastChild.nodeName); this yeild ----> sub3 _________________________________________ What I need is a way to count these children while not knowing how many there are .... if any? |
|
#2
|
|||
|
|||
|
One way you should be able to do this is as follows:
foundNodes=menu.documentElement.getElementsByTagName("*"); This will return a list and you should be able to use "foundNodes.length" to get a count of the items. If you want specific tags just substite the tag in place of the "*".
__________________
Robert Dominy About Guide for JavaScript http://javascript.about.com Software Consulting & Development http://www.angusog.com |
|
#3
|
|||
|
|||
|
rdominy
Hey....thanks for the ("*") wild card info. I have not seen used with the getElementsByTagName before. When I try this however, I get a list of all nodes in the data list......in my case...."23" . What I want is to count the children of a node. As to my example;
<catagory> <title>...... <sub1>...... <sub2>...... </catogory> How do I count the Children under the <catagory> node? In this example I would be looking for a count of three children. thanks, gary |
|
#4
|
|||
|
|||
|
Well.....I played around with the code and I found a variation that worked!!! Most of my problem has been not quite understanding the xml dom structure.....but i know more now than i did.....
Thanks again for the ("*") hint!!! gary This does what i want.....which is to count the children of a node.....note the first thread xml data list. I'm sure the code can be more consice....but hey it works! var menu; menu = new ActiveXObject("MSXML.DOMDocument"); menu.load("TestDataXml.xml"); var testNode; testNode = menu.documentElement.firstChild; var findNodes; findNodes2 = testNode.getElementsByTagName("*"); findNodesCount2 = findNodes2.length; document.write(findNodesCount2 + "<br>"); document.write(findNodes2.item(2).nodeName + "<br>") document.write("<hr>"); var i; for (i = 0; i < findNodesCount2; i++) { document.write(findNodes2.item(i).nodeName + " - "); document.write(findNodes2.item(i).firstChild.nodeValue + "<br>"); } |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > XML Node counting problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|