Dear all

I have this xml file datechanged.xml:

- <changedate>
<date>04/02/2006</date>
</changedate>

In a Javascript file I have folowing code:

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") ;
xmlDoc.async="false" ;
xmlDoc.load("datechanged.xml") ;
xmlObj=xmlDoc.documentElement ;
sDate = xmlObj.childNodes(0).firstChild.text;
var chDate=new Date(sDate) ;
I alwas get this error:

xmlObj is null or is not an object

change new ActiveXObject("Microsoft.XMLDOM") ;

to new ActiveXObject("msxml2.DOMDocument") ;

thanks for the reply,
but the same error occurs even after your suggested change:

xmlObj is null or is not an object

you might also like to consider incorporating Firefox support with the XMLHttpRequest object.

thanks for the reply,
but this is above my comprehension...

Hi,

What you have works for me in IE7.0 - Which doesn't mean it will work in IE6, but I will presume it does and that you actually want it to work in Mozilla.

In which case, you need to use the XMLHttpRequest Object...

var xmlDoc = null;

if (window.XMLHttpRequest) { // Mozilla, Safari,...
	xmlDoc = new XMLHttpRequest();
	if (xmlDoc.overrideMimeType) {
		xmlDoc.overrideMimeType('text/xml');
	}
} else if (window.ActiveXObject) { // IE
	try {
		xmlDoc = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	}
}
xmlDoc.onreadystatechange = function() {
	if(xmlDoc.readyState==4) {
		xmlObj = xmlDoc.responseXML.getElementsByTagName('changedate').item(0);

		var sDate = xmlObj.getElementsByTagName('date').item(0).firstChild.data;

		var chDate=new Date(sDate);
		alert(chDate);
	}
}
xmlDoc.open("GET", "datechanged.xml", true);
xmlDoc.send(null);

This doesn't work on my IE7.0 - But I'm assuming it won't matter much. I also consider this the cool way of loading XML data... I much less cool way of doing this is:

var xmlDoc
function loadXML()
{
//Code for IE
if (window.ActiveXObject)
  {
  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load("datechanged.xml");
  getmessage()
  }
//Code for Mozilla
else if (document.implementation && document.implementation.createDocument)
  {
  xmlDoc=document.implementation.createDocument("","",null);
  xmlDoc.load("datechanged.xml");
  xmlDoc.onload=getmessage
  }
else
  {
  alert('Your browser cannot handle this script');
  }
}

function getmessage()
{
	alert(xmlDoc.getElementsByTagName('changedate').item(0).getElementsByTagName('date').item(0).firstChild.data);
}

loadXML();

The second example works on both Firefox 1.0.5 and IE7.0

This article has been dead for over six months. Start a new discussion instead.