954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Error reading XML file in Javascript

Dear all

I have this xml file datechanged.xml:

- 04/02/2006

In a Javascript file I have folowing code:
[INDENT]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) ;[/INDENT]I alwas get this error:
[INDENT]xmlObj is null or is not an object[/INDENT]

Walfort d'Ax
Newbie Poster
5 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

change new ActiveXObject("Microsoft.XMLDOM") ;

to new ActiveXObject("msxml2.DOMDocument") ;

alpha_foobar
Junior Poster
182 posts since May 2005
Reputation Points: 20
Solved Threads: 5
 

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

alpha_foobar
Junior Poster
182 posts since May 2005
Reputation Points: 20
Solved Threads: 5
 

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

Walfort d'Ax
Newbie Poster
5 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 
you might also like to consider incorporating Firefox support with the XMLHttpRequest object.

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

Walfort d'Ax
Newbie Poster
5 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

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

alpha_foobar
Junior Poster
182 posts since May 2005
Reputation Points: 20
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You