Click to See Complete Forum and Search --> : Having Problem in Fetching Child nodes of XML File through Javascript Function


shahzeb_khan
February 24th, 2006, 07:06 AM
Dear all I have made a Function that fetches data from child Node but it throwing Exception on Line # 4 that is
on var totalchildnodes function is given below




function popuphelp(filename)
{

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.load("Help/HelpFilePages.xml");
xmlObj=xmlDoc.documentElement;

var totalchilds=xmlObj.childNodes.length; //Getting <HelpPages> Child Nodes
var mcounter=0,subloopcount=0;
alert(totalchilds);

for (mcounter=0; mcounter<=totalchilds-1; mcounter++)
{
if (xmlObj.childNodes(mcounter).tagName==filename)//Comparing the given .net File with Child Nodes
{
if (xmlObj.childNodes(mcounter).hasChildNodes()==true)//Checking Child Nodes has Sub Child Nodes
{
var subNodelen=xmlObj.childNodes(mcounter).childNodes.length; //Getting Total # of Sub Nodes
for (subloopcount=0; subloopcount<=subNodelen-1; subloopcount++)
{
window.open(xmlObj.childNodes(mcounter).childNodes(subloopcount).getAttribute("id"),xmlObj.childNodes(mcounter).childNodes(subloopcount).tagName);
}
}
}
}
}

visualAd
February 26th, 2006, 06:29 PM
By default, the document loads asynchronously (meaning the script continues straight after load is called, even if the XML isn't loaded). This is useful if you are fetching the doument over a slow connection because it mens the user doesn't have to wait around while it loads.

The reason the exception is thrown is because the document has not fully loaded. You can solve this one of two ways:

Switch to synchronous mode, whereby the script waits until the xml document is fully loaded before continuing. To do this simply set the async property to false just before calling load:

xmlDoc.async = false;
xmlDoc.load("Help/HelpFilePages.xml");

Continue using asynchronous mode and wait for the onreadystatechange event to fire with a ready state of 4, then execute the code which you want to execute once the document has loaded.

xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) {
xmlObj=xmlDoc.documentElement;

var totalchilds=xmlObj.childNodes.length; //Getting <HelpPages> Child Nodes
....
...
}
}

xmlDoc.load("Help/HelpFilePages.xml");

N.b: in Firefox you need to use the onload event instead.