943,997 Members | Top Members by Rank

Ad:
Apr 2nd, 2006
0

Error reading XML file in Javascript

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Walfort d'Ax is offline Offline
5 posts
since Apr 2006
Apr 2nd, 2006
0

Re: Error reading XML file in Javascript

change new ActiveXObject("Microsoft.XMLDOM") ;

to new ActiveXObject("msxml2.DOMDocument") ;
Reputation Points: 20
Solved Threads: 5
Junior Poster
alpha_foobar is offline Offline
182 posts
since May 2005
Apr 2nd, 2006
0

Re: Error reading XML file in Javascript

you might also like to consider incorporating Firefox support with the XMLHttpRequest object.
Reputation Points: 20
Solved Threads: 5
Junior Poster
alpha_foobar is offline Offline
182 posts
since May 2005
Apr 3rd, 2006
0

Re: Error reading XML file in Javascript

Quote originally posted by alpha_foobar ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Walfort d'Ax is offline Offline
5 posts
since Apr 2006
Apr 3rd, 2006
0

Re: Error reading XML file in Javascript

Quote originally posted by alpha_foobar ...
you might also like to consider incorporating Firefox support with the XMLHttpRequest object.
thanks for the reply,
but this is above my comprehension...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Walfort d'Ax is offline Offline
5 posts
since Apr 2006
Apr 3rd, 2006
0

Re: Error reading XML file in Javascript

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...

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. var xmlDoc = null;
  3.  
  4. if (window.XMLHttpRequest) { // Mozilla, Safari,...
  5. xmlDoc = new XMLHttpRequest();
  6. if (xmlDoc.overrideMimeType) {
  7. xmlDoc.overrideMimeType('text/xml');
  8. }
  9. } else if (window.ActiveXObject) { // IE
  10. try {
  11. xmlDoc = new ActiveXObject("Msxml2.XMLHTTP");
  12. } catch (e) {
  13. try {
  14. xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
  15. } catch (e) {}
  16. }
  17. }
  18. xmlDoc.onreadystatechange = function() {
  19. if(xmlDoc.readyState==4) {
  20. xmlObj = xmlDoc.responseXML.getElementsByTagName('changedate').item(0);
  21.  
  22. var sDate = xmlObj.getElementsByTagName('date').item(0).firstChild.data;
  23.  
  24. var chDate=new Date(sDate);
  25. alert(chDate);
  26. }
  27. }
  28. xmlDoc.open("GET", "datechanged.xml", true);
  29. 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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. var xmlDoc
  3. function loadXML()
  4. {
  5. //Code for IE
  6. if (window.ActiveXObject)
  7. {
  8. xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  9. xmlDoc.async=false;
  10. xmlDoc.load("datechanged.xml");
  11. getmessage()
  12. }
  13. //Code for Mozilla
  14. else if (document.implementation && document.implementation.createDocument)
  15. {
  16. xmlDoc=document.implementation.createDocument("","",null);
  17. xmlDoc.load("datechanged.xml");
  18. xmlDoc.onload=getmessage
  19. }
  20. else
  21. {
  22. alert('Your browser cannot handle this script');
  23. }
  24. }
  25.  
  26. function getmessage()
  27. {
  28. alert(xmlDoc.getElementsByTagName('changedate').item(0).getElementsByTagName('date').item(0).firstChild.data);
  29. }
  30.  
  31. loadXML();

The second example works on both Firefox 1.0.5 and IE7.0
Reputation Points: 20
Solved Threads: 5
Junior Poster
alpha_foobar is offline Offline
182 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Call a external js function into another js function
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Explorer Blocking Script





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC