XML DOM Parsing, What if you don't know what the file looks like?

Discussion in 'JavaScript' started by execute, Dec 14, 2005.

  1. #1
    I have been trying something like this, but wasn't able to get it right, as i always got errors. I am trying to recieve an XML document but i don't know what exactly it will look like or how many childs it has, and what not. So the tree is sort of like:
    - ffff
    __-fff
    ___-fffff
    ___-fffff
    ___-fffff
    _____-ff
    _____-ff
    ___-fffff
    __-fff
    __+fff
    __+fff

    In other words its a complex XML file, and you don't know how it's gonna turn out. I have been trying to find a way to test for child nodes, i mean if it has a child then go there, if not stick in the upper level. But i'm not sure how to navigate thru the DOM.

    for(i = 0; i < XMLobj.childNodes.length; i++){
              try {
                debug("Attempting First Child");
                XML.push(XMLobj.childNodes[i].firstchild.nodeName+': '+XMLobj.childNodes[i].firstchild.nodeValue);
              } catch(e){
                debug("Attemting Second child - Error: "+e);
                try {
                  XML.push(XMLobj.childNodes[0].childNodes[i].firstchild.nodeName+': '+XMLobj.childNodes[0].childNodes[i].firstchild.nodeValue);
                } catch(e) {
                  debug("Second Child Node Failed: "+e);
                  try {
                    XML.push(XMLobj.childNodes[0].childNodes[0].childNodes[i].firstchild.nodeName+': '+XMLobj.childNodes[0].childNodes[0].childNodes[i].firstchild.nodeValue);
                  } catch(e) {
                    debug("Third Child Node Attempted, Failed: "+e);
                  }
                }
              }
            }
    Code (markup):
    If anyone already has a function. Please do tell
    But basically i want a DOM script to actually parse all nodeNames and set them equal to the value and display them like a tree or just plain bullets.
     
    execute, Dec 14, 2005 IP