Connecting Tech Pros Worldwide Help | Site Map

Problem Reading XML File with Firefox

Newbie
 
Join Date: Apr 2007
Posts: 1
#1: Apr 16 '07
The xml and javascript are below.
IE finds everything fine, Firefox gives the following error -
xmlDoc.getElementsByTagName("lab_name")[0] has no properties
on this line -
alert(xmlDoc.getElementsByTagName('lab_name')[0].childNodes[0].nodeValue);

I am using Firebug to help me debug. When I look at the DOM right after the page loads the childNodes part of my xmlDoc tree is empty, this explains why I get the error.
But why is it empty????!!!!!
It should have the xml structure in it.

To top it off, if I close the childNodes part of the tree (hide it) and then show it again by clicking the arrow to the left of it, Firebug finds everything!!!!!!!!!!!!!!!

All help is greatly appreciated!

XML File Example :
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <lab>
  3. <lab_name>My Lab</lab_name>
  4. <about>
  5. <description><![CDATA[A paragraph describing my lab!]]></description>
  6. <image>images/path/myimage.jpg</image>
  7. </about>
  8. </lab>
Javascript To Read It :
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. if (window.ActiveXObject)
  3. {
  4. xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  5. xmlDoc.async=false;
  6. xmlDoc.load("xml/test_xml.xml");
  7. alert(xmlDoc.getElementsByTagName('lab_name')[0].childNodes[0].nodeValue);
  8. alert(xmlDoc.getElementsByTagName('description')[0].childNodes[0].nodeValue);
  9. alert(xmlDoc.getElementsByTagName('image')[0].childNodes[0].nodeValue);
  10. }
  11. // code for Mozilla, Firefox, Opera, etc.
  12. else if (document.implementation && document.implementation.createDocument)
  13. {
  14. xmlDoc = document.implementation.createDocument("", "", null);
  15. xmlDoc.load("xml/test_xml.xml");
  16. alert(xmlDoc.getElementsByTagName('lab_name')[0].childNodes[0].nodeValue);
  17. alert(xmlDoc.getElementsByTagName('description')[0].childNodes[0].nodeValue);
  18. alert(xmlDoc.getElementsByTagName('image')[0].childNodes[0].nodeValue);
  19. }
  20. // no good browser found
  21. else
  22. {
  23. if (!xmlDoc.load("xml/test_xml.xml"))
  24. {
  25. alert ("Failed to load XML data source!");
  26. }
  27. }
dorinbogdan's Avatar
Expert
 
Join Date: Feb 2007
Posts: 822
#2: Apr 17 '07

re: Problem Reading XML File with Firefox


Welcome to TheScripts TSDN....

Try this way:
Expand|Select|Wrap|Line Numbers
  1.         if (window.ActiveXObject)
  2.         {
  3.         xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  4.         xmlDoc.async=false;
  5.         xmlDoc.load("test_xml.xml");
  6.         testNodes();
  7.         }
  8.         // code for Mozilla, Firefox, Opera, etc.
  9.         else if (document.implementation && document.implementation.createDocument)
  10.         {
  11.         xmlDoc = document.implementation.createDocument("", "", null);
  12.         xmlDoc.load("test_xml.xml");
  13.         xmlDoc.onload = testNodes;
  14.         }
  15.         // no good browser found
  16.         else
  17.         {
  18.         if (!xmlDoc.load("test_xml.xml"))
  19.         {
  20.         alert ("Failed to load XML data source!");
  21.         }
  22.         }
  23.  
  24.     function testNodes(){
  25.         alert(xmlDoc.getElementsByTagName('lab_name')[0].childNodes[0].nodeValue);
  26.         alert(xmlDoc.getElementsByTagName('description')[0].childNodes[0].nodeValue);
  27.         alert(xmlDoc.getElementsByTagName('image')[0].childNodes[0].nodeValue);
  28.     }
  29.  
Reply