My Personal Online Recycle Bin
You may find something useful here...........

Wednesday, December 14, 2005

Traversing XML DOM Document - IE and Netscape Problem.

Yesterday, i was facing a problem to traverse a xml DOM document.

The problem was the java script code work differently in IE and netscape 6/Mozilla. The Code is as follow:

nodeBody[0].childNodes[j-1].firstChild.nodeValue;

The code work fine in IE but not in netscape 6 / mozilla. But if the code is change to

nodeBody[0].childNodes[j-2].firstChild.nodeValue;

Then it work in netscape but not IE. So I feel very odd. And wanna to find out why so.

After a quick search on google, i found the answer to my question and solution to the above problem.

Why same code behave differently in IE and netscape?


This is because in NS6 (and Mozilla), white spaces are considered elements as well (TEXT), while in IE, they are not.


Solution to this problem is as follow

//Regular expression used to match any non-whitespace character
var notWhitespace = /\S/

function removeWhiteSpace(){
//Cache "messages" element of xml file
var msgobj=xmlDoc.getElementsByTagName("messages")[0]

//REMOVE white spaces in XML file. Intended mainly for NS6/Mozilla
for (i=0;i<msgobj.childNodes.length;i++){
if ((msgobj.childNodes[i].nodeType == 3)& &
(!notWhitespace.test(msgobj.childNodes[i].nodeValue))) {
// that is, if it's a whitespace text node
msgobj.removeChild(msgobj.childNodes[i])
i--
}
}
}


For the full article on this issue, you can refer here.

1 Comments:

Post a Comment

<< Home