Not able to load xml file in IE8

2019-08-27 17:09发布

问题:

I'm trying to find the solution to load xml file and retrieve element value by tag name but stuck at the final step. Code is working in all other modern browsers except IE8.

function getXML(xmlStr) {
    if (window.DOMParser) {
       alert("window.DOMParser");
       return (new window.DOMParser()).parseFromString(xmlStr, "text/xml");
    } else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
       alert("Microsoft.XMLDOM");
       var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
       xmlDoc.async = "false";
       xmlDoc.loadXML(xmlStr);
       alert("xmlDoc");
       return xmlDoc;
    } else {
       return null;
  }
}

$(document).ready(function() {
  var xmlStr = "<xml><elem>element1</elem><elem>element2</elem></xml>";
  var xmlDoc = getXML(xmlStr);
  $("#result").html("<b>Elemtent:</b> " + JSON.stringify(xmlDoc) + "<br/><br>");
});

I've created jsfiddle here which is the modification of the actual post answer by Tim Down.

I've tried many ways but no luck at all. Is it possible to load xml and get values in IE8?