Advertisement

05.05.2008 at 06:03AM PDT, ID: 23376243
[x]
Attachment Details

Parse XML in firefox using javascript

Asked by stivinu in JavaScript, Firefox Web Browser

Tags: XML, Firefox, javascript, Firefox

Hi,

I am trying to parse an XML file in order to read data from the XML file.  I am using an Xpath expression in javascript in order to parse the XML file.  Everything works fine in INternet Explorer where I manage to parse the XML file and get back an answer from this file, however I am having problems doing so in Firefox.  I am not able to get any type of answer from the XML file when I use Firefox.

This is my code in javascript:

    <script type="text/javascript">
        var searchString = document.location.search;
        searchString = searchString.substring(1);
        var nvPairs = searchString.split("&");
        for (i = 0; i < nvPairs.length; i++)
        {
            var nvPair = nvPairs[i].split("=");
            var name = nvPair[0];
            var value = nvPair[1];
            if (i == 0) {
                var time = value;
            }
            if (i == 1) {
                var type = value;
            }
            if (i == 2) {
                var soil = value;
            }
        }
        function loadXMLDoc(fname)
        {
            var xmlDoc;
            // code for IE
            if (window.ActiveXObject)
            {

                xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            }
            // code for Mozilla, Firefox, Opera, etc.
            else if (document.implementation && document.implementation.createDocument)
            {
           
                xmlDoc=document.implementation.createDocument("","",null);
            }
            else
            {
                alert('Your browser cannot handle this script');
            }
            xmlDoc.async=false;
            xmlDoc.load(fname);
            return(xmlDoc);
        }
        xml=loadXMLDoc("Crops to grow.xml");
        path="/Start/month[@on='" + time + "']/product[@id='" + type + "']/land[@type='" + soil + "']/grow";
        // code for IE
        if (window.ActiveXObject)
        {
            var nodes=xml.selectNodes(path);
            for (i=0;i<nodes.length;i++)
            {
                document.write("PRODUCTS TO GROW: ",nodes[i].childNodes[0].nodeValue);
                document.write("<br /> <br />");
            }
        }
        // code for Mozilla, Firefox, Opera, etc.
        else if (document.implementation && document.implementation.createDocument)
        {
           
            var nodes=document.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
            var result=nodes.iterateNext();
            while (result)
            {
                document.write(result.childNodes[0].nodeValue);
                document.write("<br />");
                result=nodes.iterateNext();
            }
        }
    </script>

It appears that the code which has to do with the Firefox browser is being neglected by javascipt!! Could someone please help me to identify the problem.

ThanksStart Free Trial
[+][-]05.05.2008 at 08:39AM PDT, ID: 21500799

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.06.2008 at 02:58AM PDT, ID: 21505927

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.08.2008 at 01:51AM PDT, ID: 21523240

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: JavaScript, Firefox Web Browser
Tags: XML, Firefox, javascript, Firefox
Sign Up Now!
Solution Provided By: stivinu
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628