#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Texas
    Posts
    18
    Rep Power
    0

    Unhappy


    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?

    Comments on this post

    • Gran Roguismo agrees
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    81
    Rep Power
    14
    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 "*".

    Comments on this post

    • Gran Roguismo agrees
    Robert Dominy
    About Guide for JavaScript
    http://javascript.about.com
    Software Consulting & Development
    http://www.angusog.com
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Texas
    Posts
    18
    Rep Power
    0

    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
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Texas
    Posts
    18
    Rep Power
    0

    Thumbs up rdominy.....again


    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>");
    }

IMN logo majestic logo threadwatch logo seochat tools logo