dbrownell83
asked on
need help with simple recursive DOM parsing
Hi experts,
I have been looking through the archives, and almost have a solution to my problem, but not really. The purpose is to fill a YAHOO treeview with my XML data. In the code below, doc is a well formed XML document.
var rootNode=doc.documentEleme nt;
this.readChildrenIntoTree( rootNode.c hildNodes, tree.getRoot());
tree.draw();
readChildrenIntoTree:funct ion(nodeLi st, treeNode) {
for (x in nodeList)
{
alert(x.nodeValue);
var tmpNode = new YAHOO.widget.TextNode(x.no deValue, treeNode, false);
if(nodeList.hasChildNodes)
readChildrenIntoTree(nodeL ist.childN odes, tmpNode);
}
}
The alert says "undefined", which is quite certainly related to the problem. How can I get the <x>nodeValue</x>?
And is my recursive code looking more or less correct?
Thanks
Dan
I have been looking through the archives, and almost have a solution to my problem, but not really. The purpose is to fill a YAHOO treeview with my XML data. In the code below, doc is a well formed XML document.
var rootNode=doc.documentEleme
this.readChildrenIntoTree(
tree.draw();
readChildrenIntoTree:funct
for (x in nodeList)
{
alert(x.nodeValue);
var tmpNode = new YAHOO.widget.TextNode(x.no
if(nodeList.hasChildNodes)
readChildrenIntoTree(nodeL
}
}
The alert says "undefined", which is quite certainly related to the problem. How can I get the <x>nodeValue</x>?
And is my recursive code looking more or less correct?
Thanks
Dan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks, i copied someone's js code thinking it was some special foreach syntax. oops.
readChildrenIntoTree:funct ion(nodeLi st, treeNode) {
for (var x=0; x < nodeList.length; x++)
{
var node = nodeList[x];
alert(node);
alert(node.nodeValue);
alert(node.nodeName);
var tmpNode = new YAHOO.widget.TextNode(node , treeNode, false);
if(node.hasChildNodes)
readChildrenIntoTree(node. childNodes , tmpNode);
}
}
This prints out:
node = [object Element]
node.nodeValue = null
node.nodeName = "child"
Hmm...
my XML has this sort of look:
<categories>
<child>Engineering
<child>AeroSpace Engineering</child>
<child>Computer Engineering</child>
<child>Electrical Engineering</child>
</child>
</categories>
Does it matter that my inner nodes have the same tag as the outer ones?
readChildrenIntoTree:funct
for (var x=0; x < nodeList.length; x++)
{
var node = nodeList[x];
alert(node);
alert(node.nodeValue);
alert(node.nodeName);
var tmpNode = new YAHOO.widget.TextNode(node
if(node.hasChildNodes)
readChildrenIntoTree(node.
}
}
This prints out:
node = [object Element]
node.nodeValue = null
node.nodeName = "child"
Hmm...
my XML has this sort of look:
<categories>
<child>Engineering
<child>AeroSpace Engineering</child>
<child>Computer Engineering</child>
<child>Electrical Engineering</child>
</child>
</categories>
Does it matter that my inner nodes have the same tag as the outer ones?
ASKER
erm yeah... it isnt properly formed. i'll ask on the xml board. thanks
thank you for the points!
you should know, however, that for...in *IS* valid syntax in JS, for example "for (var i in arrayList)". the nodelist object doesn't happen to support it.
- hbz
The for loop you had was foremed poorly, syntax not javascript. The "undefined" was printing because you didn't declare x as a variable, so the script couldn't find a variable called x.
Best Wishes,
Jesse