如何加载和使用Github中托管的XML文件

时间:2019-03-07 12:19:20

标签: javascript xml github

下面的代码有效,但是当我将XML文件activities.xml的相对路径位置从Gitbub https://raw.githubusercontent.com/Podatus/web-files/master/activities.xml更改为绝对文件时,它不起作用,出现此错误:

  

TypeError:XSLTProcessor.transformToFragment的参数1不是对象。

看起来他无法加载XML

function loadXMLDoc(filename){
  if (window.ActiveXObject){
    xhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }
  else {
    xhttp = new XMLHttpRequest();
  }
  xhttp.open("GET", filename, false);
  try {xhttp.responseType = "msxml-document"} catch(err) {} // Helping IE11
  xhttp.send("");
  return xhttp.responseXML;
}

function displayResult(xslFileName){
  xml = loadXMLDoc("activities.xml");
  xsl = loadXMLDoc(xslFileName);
  // code for IE
  if (window.ActiveXObject || xhttp.responseType == "msxml-document") {
    ex = xml.transformNode(xsl);
    document.getElementById("showData").innerHTML = ex;
    }
  // code for Chrome, Firefox, Opera, etc.
  else if (document.implementation && document.implementation.createDocument) {
    xsltProcessor = new XSLTProcessor();
    xsltProcessor.importStylesheet(xsl);
    var temp = document.getElementById("showData") ;
    while(temp.firstChild){ // delete all ancient data
      temp.removeChild(temp.firstChild);
      resultDocument = xsltProcessor.transformToFragment(xml, document);
    }
    document.getElementById("showData").appendChild(resultDocument);
  }
}

0 个答案:

没有答案