2014-11-21 57 views
0
的更換使用

我有一些遺留代碼工作在許多地方有代碼從某些網址,以獲取XML數據。這非常簡單。加載XML到DOM與Microsoft.XMLDOM

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
xmlDoc.async="false";   
xmlDoc.load(url); 

,並在一些地方

var httpRequest= new ActiveXObject("Msxml2.XMLHTTP.6.0"); 
httpRequest.open('GET', url, false); 
httpRequest.send(null); 

在大多數情況下,我們從響應XML採摘結果。 我在很多地方看到,「Microsoft.XMLDOM」的用法已過時,取而代之的是ActiveXObject(「Msxml2.XMLHTTP.6.0」)。 對於其他瀏覽器,我應該使用標準的W3C XMLHttpRequest。這似乎沒有任何問題。

問題加載結果XML字符串。 我看到loadXML是使用「Microsoft.XMLDOM」而不是ActiveXObject(「Microsoft.XMLHTTP」)定義的;

隨着其他瀏覽器的DOMParser是所提出的方法以及IE-11。

這是我從url中檢索信息,然後 解析該信息,然後嘗試將XML字符串加載到DOM。 我的主要問題是,在處理與Internet Explorer有關的XML時,或者在一天中爲時太晚,我越來越困惑於什麼解決方案是合適的。 我想刪除「Microsoft.XMLDOM」的用法,但要執行loadXML,我必須回到它。有沒有更好的方法來解決這個問題?

// Get the information use either the XMLHttpRequest or ActiveXObject 
if (window.ActiveXObject || 'ActiveXObject' in window) { 
     httpRequest = new ActiveXObject("Msxml2.XMLHTTP.6.0"); 
     } 
     else if (window.XMLHttpRequest) { 
      httpRequest = new XMLHttpRequest(); 
      if (httpRequest.overrideMimeType) { 
       httpRequest.overrideMimeType('text/xml'); 
    } 
} 

httpRequest.open('GET', url, false); 
httpRequest.send(); 

var xmlDoc = httpRequest.responseXML; 

// Retrieve the XML into the DOM 
var xml = xmlDoc.getElementsByTagName("XML_STRING_SETTINGS") 


// Load the XML string into the DOM 
if (window.DOMParser) { 
    var parser = new DOMParser(); 
    xmlDoc = parser.parseFromString(xml, "text/xml"); 
} 
else // code for IE 
{ 
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
    xmlDoc.async = false; 
    // is there another way to do this load? 
    xmlDoc.loadXML(xml); 
} 

回答

0

我認爲IE5和代碼6

new ActiveXObject("Microsoft.XMLHTTP") 

而對於IE7 +(和瀏覽器的其餘部分)

new XMLHttpRequest();