Error reading XML file in Javascript

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Apr 2006
Posts: 5
Reputation: Walfort d'Ax is an unknown quantity at this point 
Solved Threads: 0
Walfort d'Ax Walfort d'Ax is offline Offline
Newbie Poster

Error reading XML file in Javascript

 
0
  #1
Apr 2nd, 2006
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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: Error reading XML file in Javascript

 
0
  #2
Apr 2nd, 2006
change new ActiveXObject("Microsoft.XMLDOM") ;

to new ActiveXObject("msxml2.DOMDocument") ;
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: Error reading XML file in Javascript

 
0
  #3
Apr 2nd, 2006
you might also like to consider incorporating Firefox support with the XMLHttpRequest object.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5
Reputation: Walfort d'Ax is an unknown quantity at this point 
Solved Threads: 0
Walfort d'Ax Walfort d'Ax is offline Offline
Newbie Poster

Re: Error reading XML file in Javascript

 
0
  #4
Apr 3rd, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5
Reputation: Walfort d'Ax is an unknown quantity at this point 
Solved Threads: 0
Walfort d'Ax Walfort d'Ax is offline Offline
Newbie Poster

Re: Error reading XML file in Javascript

 
0
  #5
Apr 3rd, 2006
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...
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: Error reading XML file in Javascript

 
0
  #6
Apr 3rd, 2006
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC