views:

344

answers:

1

I use the following code to open an XML document. It works in Firefox and IE, but fails in Safari. Any idea why?

function crearObjetoXML(archivoXML){
  //--- IE.
  if(window.ActiveXObject){
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.load(archivoXML);
    parsearXML();
  //--- FF.
  }else if(document.implementation && document.implementation.createDocument){
    xmlDoc = document.implementation.createDocument("","",null);
    xmlDoc.load(archivoXML);
    xmlDoc.onload = parsearXML;
  }else{
    alert ('Su navegador no puede soportar este script');
  }
}


function parsearXML(){
  numrows = xmlDoc.getElementsByTagName('advertise')[0].childNodes.length;
  lnks1 = new Array(numrows);
  for (var i=0;i<=numrows-1;i++)
  {
    lnks1[i] =  xmlDoc.getElementsByTagName('advertise')[0].getElementsByTagName('item')[i].getAttribute('link'); 
  }

}
crearObjetoXML('../imagerotatorxml.php');
A: 

thanks ysth it was helpful your comment to solved the problem i will put my code if someone finds it useful the problem was with this: document.implementation.createDocument("","",null); Firefox creates an XML document but Safari just create a document when it reaches this part: xmlDoc.load it fails cause safari doesn't recognize this so in the code if the try fails it means that its safari then enters catch and use the right functions for safari and everything else its the same.

var xmlDoc;
function crearObjetoXML(archivoXML){
  //---this is for IE.
  if(window.ActiveXObject){
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.load(archivoXML);
    parsearXML();

  }else if(document.implementation && document.implementation.createDocument){
    try{//--- this is for FF, opera and others.
            xmlDoc = document.implementation.createDocument("","",null);
            xmlDoc.load(archivoXML);
            xmlDoc.onload = parsearXML; 
       }
       catch(e){// if the other one fails enters here for Safari                        
            xmlDoc = new XMLHttpRequest();                       
            xmlDoc.open("GET", archivoXML, false);                        
            xmlDoc.send();                        
            xmlDoc=xmlDoc.responseXML; 
            parsearXML(); 
  }else{
    alert ('Su navegador no puede soportar este script');
  }
}


function parsearXML(){
  numrows = xmlDoc.getElementsByTagName('advertise')[0].childNodes.length;
  lnks1 = new Array(numrows);
  for (var i=0;i<=numrows-1;i++)
  {
    lnks1[i] =  xmlDoc.getElementsByTagName('advertise')[0].getElementsByTagName('item')[i].getAttribute('link'); 
  }

}
crearObjetoXML('../imagerotatorxml.php');
pepelucaz