Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Thursday, November 20, 2008

XML Dom in chrome

As all know we usually use this ActiveXObject for IE like this:

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.onreadystatechange=verify;
xmlDoc.load(xmlFile);

but problem comes when i try to run same code in Mozilla or chrome

then find that Mozilla problem can be solved by following code:

xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load(xmlFile);
xmlObj=xmlDoc.documentElement;
var tnode = xmlObj.childNodes.length;

for(e=0;e {
text = xmlObj.childNodes[e].childNodes[0].firstChild.nodeValue;
value =xmlObj.childNodes[e].childNodes[1].firstChild.nodeValue;
}

But again I stuck while calling same code file in chrome

So after searching a lot, I found this better way as it supports both Mozilla and chrome browser

var xmlDoc= new window.XMLHttpRequest();
if(xmlDoc.overrideMimeType)
{
xmlDoc.overrideMimeType('text/xml');
}
xmlDoc.open("GET",url,false);
xmlDoc.send(null);
var xmlDoc1 = xmlDoc.responseXML;
var x=xmlDoc1.getElementsByTagName("item");
for(e=0;e {
var text = x[e].getElementsByTagName("category")[0].textContent;
var value =x[e].getElementsByTagName("id")[0].textContent;
alert(text);
alert(value);
}

Look the code above you can notice that in place of nodevalue we use textcontent as nodevalue give u null value.

Hope this will work out.
Enjoy :)