Trying to merge three xml files with Javascript successful in IE but having problems with Firefox

Hi,

I’m trying to merge three xml files in memory. I’ve been successful with IE but the Firefox routine is being problematic. My intention is to merge the ‘B & C’ files into the ‘A’ file. All seems to work well until I get to the second ‘iterateNext’ statement, at which point I receive the following error from Firefox: “An attempt was made to use an object that is not, or is no longer, usable code: 11”.

A snippet is as follows:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Portfolio Management 2.0 --</title>


<script>
      
      var A_doc;
      var B_doc;
      var C_doc;
      
      var A_file;
      var B_file;
      var C_file;
      
      var A_svar;
      var B_svar;
      var C_svar;

      var XML = new Object;


      

      A_file       = "A.xml"
      B_file       = "B.xml"
      C_file       = "C.xml"      


      function loadXMLDoc(fname)
      {
            var xmlDoc;
            // code for IE
            if (window.ActiveXObject) {
                    //xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                  xmlDoc=new ActiveXObject("Msxml2.DOMDocument");
              }
            // 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);
      }

      function mergeFiles()
      {      
      
            A_doc       = loadXMLDoc(A_file);
            B_doc       = loadXMLDoc(B_file);
            C_doc       = loadXMLDoc(C_file);
            
            // code for IE
            if (window.ActiveXObject) {
                A_doc.sBroperty("SelectionLanguage","XPath");
                B_doc.sBroperty("SelectionLanguage","XPath");
                C_doc.sBroperty("SelectionLanguage","XPath");
                
                var sysnodes = A_doc.selectNodes("data/systems/system");
                
                if (sysnodes.length > 0)
                {      
                      // Spin through the System Nodes
                      for (i=0; i<sysnodes.length; i++){
                            A_svar      = sysnodes[i].getElementsByTagName("s_var")[0].childNodes[0].nodeValue;
                            
                            //  Add in the associated B Nodes
                            var Bnodes = B_doc.selectNodes("data/systems/system[s_var=" + A_svar +"]/mlstns");
                            if (Bnodes.length > 0 ){
                            var x1 = sysnodes[i];
                            x1.appendChild(Bnodes[0]);}
                            
                            var Cnodes = C_doc.selectNodes("data/systems/system[s_var=" + A_svar + "]/budg");
                            if (Cnodes.length > 0 ){
                            var x2 = sysnodes[i];
                            x2.appendChild(Cnodes[0]);}
                            
                            var Cnodes = C_doc.selectNodes("data/systems/system[s_var=" + A_svar + "]/actl");
                            if (Cnodes.length > 0 ){
                            var x3 = sysnodes[i];
                            x3.appendChild(Cnodes[0]);}
                            
                            var Cnodes = C_doc.selectNodes("data/systems/system[s_var=" + A_svar + "]/bes");
                            if (Cnodes.length > 0 ){
                            var x4 = sysnodes[i];
                            x4.appendChild(Cnodes[0]);}
                            
                      }
                }

              }
            // code for Mozilla, Firefox, Opera, etc.
            else
            if (document.implementation && document.implementation.createDocument) {
                  var sysNodes = A_doc.evaluate("data/systems/system",A_doc,null, XPathResult.ANY_TYPE,null);
                        

                  var system=sysNodes.iterateNext();
                  while (system)
                  {
            
                        A_svar      = system.getElementsByTagName("s_var")[0].childNodes[0].nodeValue;
                        
                        //  Add in the associated B Nodes
                        var Bnodes = B_doc.evaluate("data/systems/system[s_var=" + A_svar +"]/mlstns",B_doc,null, XPathResult.ANY_TYPE,null);
                        B = Bnodes.iterateNext();
                        if (B){
                        system.appendChild(B);}
                        
                        var Cnodes = C_doc.evaluate("data/systems/system[s_var=" + A_svar + "]/budg",C_doc,null, XPathResult.ANY_TYPE,null);
                        var C2 = Cnodes.iterateNext();
                        if (C2){
                        system.appendChild(C2);}
                        
                        var Cnodes = C_doc.evaluate("data/systems/system[s_var=" + A_svar + "]/actl",C_doc,null, XPathResult.ANY_TYPE,null);
                        var C3 = Cnodes.iterateNext();
                        if (C3){
                        system.appendChild(C3);}
                        
                        var Cnodes = C_doc.evaluate("data/systems/system[s_var=" + A_svar + "]/bes",C_doc,null, XPathResult.ANY_TYPE,null);
                        var C4 = Cnodes.iterateNext();
                        if (C4){
                        system.appendChild(C4);}
                  
                        system=sysNodes.iterateNext();      ******* Firefox gives me a Code 11  Error on this line   *****
                  }
              }
      }
                                             
</script>
</head>

<body onLoad="mergeFiles()">
<center>
<div id="xslt_as_html"></div>
</center>
<br />
</body>
</html>
FleurysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FleurysAuthor Commented:
Actually, I was able to come up with the solution on my own. Mozilla took exception to the fact that I was trying to iterate through an XML document while adding nodes to it at the same time. The solution was to create two instances of the A_doc, then iterate throuth one and update the second one. The corrected code for this procedure is as follows:


      function mergeFiles()
      {      
     
            A1_doc      = loadXMLDoc(A_file);
                  A2_doc            = loadXMLDoc(A_file);
            B_doc       = loadXMLDoc(B_file);
            C_doc       = loadXMLDoc(C_file);
           
            // code for IE
            if (window.ActiveXObject) {
                A_doc.sBroperty("SelectionLanguage","XPath");
                B_doc.sBroperty("SelectionLanguage","XPath");
                C_doc.sBroperty("SelectionLanguage","XPath");
               
                var sysnodes = A1_doc.selectNodes("data/systems/system");
               
                if (sysnodes.length > 0)
                {      
                      // Spin through the System Nodes
                      for (i=0; i<sysnodes.length; i++){
                            A_svar      = sysnodes[i].getElementsByTagName("s_var")[0].childNodes[0].nodeValue;
                           
                            //  Add in the associated B Nodes
                            var Bnodes = B_doc.selectNodes("data/systems/system[s_var=" + A_svar +"]/mlstns");
                            if (Bnodes.length > 0 ){
                            var x1 = sysnodes[i];
                            x1.appendChild(Bnodes[0]);}
                           
                            var Cnodes = C_doc.selectNodes("data/systems/system[s_var=" + A_svar + "]/budg");
                            if (Cnodes.length > 0 ){
                            var x2 = sysnodes[i];
                            x2.appendChild(Cnodes[0]);}
                           
                            var Cnodes = C_doc.selectNodes("data/systems/system[s_var=" + A_svar + "]/actl");
                            if (Cnodes.length > 0 ){
                            var x3 = sysnodes[i];
                            x3.appendChild(Cnodes[0]);}
                           
                            var Cnodes = C_doc.selectNodes("data/systems/system[s_var=" + A_svar + "]/bes");
                            if (Cnodes.length > 0 ){
                            var x4 = sysnodes[i];
                            x4.appendChild(Cnodes[0]);}
                           
                      }
                }

              }
            // code for Mozilla, Firefox, Opera, etc.
            else
            if (document.implementation && document.implementation.createDocument) {
                  var A1_nodes = A1_doc.evaluate("data/systems/system",A1_doc,null, XPathResult.ANY_TYPE,null);
                       

                  var system=A1_nodes.iterateNext();
                  while (system)
                  {
           
                        A_svar      = system.getElementsByTagName("s_var")[0].childNodes[0].nodeValue;
                                    var A2_nodes = A2_doc.evaluate("data/systems/system[s_var=" + ditpr_svar +"]",ditpr_doc,null, XPathResult.ANY_TYPE,null);
                                    A = A2_nodes.iterateNext();
                       
                        //  Add in the associated B Nodes
                        var Bnodes = B_doc.evaluate("data/systems/system[s_var=" + A_svar +"]/mlstns",B_doc,null, XPathResult.ANY_TYPE,null);
                        B = Bnodes.iterateNext();
                        if (B){
                        A.appendChild(B);}
                       
                        var Cnodes = C_doc.evaluate("data/systems/system[s_var=" + A_svar + "]/budg",C_doc,null, XPathResult.ANY_TYPE,null);
                        var C2 = Cnodes.iterateNext();
                        if (C2){
                        A.appendChild(C2);}
                       
                        var Cnodes = C_doc.evaluate("data/systems/system[s_var=" + A_svar + "]/actl",C_doc,null, XPathResult.ANY_TYPE,null);
                        var C3 = Cnodes.iterateNext();
                        if (C3){
                        A.appendChild(C3);}
                       
                        var Cnodes = C_doc.evaluate("data/systems/system[s_var=" + A_svar + "]/bes",C_doc,null, XPathResult.ANY_TYPE,null);
                        var C4 = Cnodes.iterateNext();
                        if (C4){
                        A.appendChild(C4);}
                 
                        system=A1_nodes.iterateNext();      ******* No Error here now   *****
                  }
              }
      }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Browsers

From novice to tech pro — start learning today.