469,344 Members | 5,642 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,344 developers. It's quick & easy.

Problem Reading XML File with Firefox

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. }
Apr 16 '07 #1
1 7953
dorinbogdan
839 Expert 512MB
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.  
Apr 17 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by tagdance | last post: by
7 posts views Thread by Steven (remove wax and invalid for reply) | last post: by
3 posts views Thread by binnyva | last post: by
3 posts views Thread by cwdjrxyz | last post: by
2 posts views Thread by Gary Coutts | last post: by
1 post views Thread by paulnightingale | last post: by
34 posts views Thread by Simon Wigzell | last post: by
4 posts views Thread by charles | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.