Problem Reading XML File with Firefox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • awolfe76
    New Member
    • Apr 2007
    • 1

    #1

    Problem Reading XML File with Firefox

    The xml and javascript are below.
    IE finds everything fine, Firefox gives the following error -
    xmlDoc.getEleme ntsByTagName("l ab_name")[0] has no properties
    on this line -
    alert(xmlDoc.ge tElementsByTagN ame('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 :
    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <lab>
    <lab_name>My Lab</lab_name>
    <about>
    <description><![CDATA[A paragraph describing my lab!]]></description>
    <image>images/path/myimage.jpg</image>
    </about>
    </lab>
    Javascript To Read It :
    Code:
    <script language="javascript">
    if (window.ActiveXObject)
    {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.load("xml/test_xml.xml");
    alert(xmlDoc.getElementsByTagName('lab_name')[0].childNodes[0].nodeValue);
    alert(xmlDoc.getElementsByTagName('description')[0].childNodes[0].nodeValue);
    alert(xmlDoc.getElementsByTagName('image')[0].childNodes[0].nodeValue);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
    {
    xmlDoc = document.implementation.createDocument("", "", null);
    xmlDoc.load("xml/test_xml.xml");
    alert(xmlDoc.getElementsByTagName('lab_name')[0].childNodes[0].nodeValue);
    alert(xmlDoc.getElementsByTagName('description')[0].childNodes[0].nodeValue);
    alert(xmlDoc.getElementsByTagName('image')[0].childNodes[0].nodeValue);
    }
    // no good browser found
    else
    {
    if (!xmlDoc.load("xml/test_xml.xml"))
    {
    alert ("Failed to load XML data source!");
    }
    }
  • dorinbogdan
    Recognized Expert Contributor
    • Feb 2007
    • 839

    #2
    Welcome to TheScripts TSDN....

    Try this way:
    Code:
    		if (window.ActiveXObject)
    		{
    		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    		xmlDoc.async=false;
    		xmlDoc.load("test_xml.xml");
    		testNodes();
    		}
    		// code for Mozilla, Firefox, Opera, etc.
    		else if (document.implementation && document.implementation.createDocument)
    		{
    		xmlDoc = document.implementation.createDocument("", "", null);
    		xmlDoc.load("test_xml.xml");
    		xmlDoc.onload = testNodes;
    		}
    		// no good browser found
    		else
    		{
    		if (!xmlDoc.load("test_xml.xml"))
    		{
    		alert ("Failed to load XML data source!");
    		}
    		}
    
    	function testNodes(){
    		alert(xmlDoc.getElementsByTagName('lab_name')[0].childNodes[0].nodeValue);
    		alert(xmlDoc.getElementsByTagName('description')[0].childNodes[0].nodeValue);
    		alert(xmlDoc.getElementsByTagName('image')[0].childNodes[0].nodeValue);
    	}

    Comment

    Working...