如何使用jquery或javascript从URL链接中提取XML数据

时间:2018-04-25 14:41:50

标签: javascript jquery xml

我正在尝试重建我在python中使用的一些东西,但我发现从API链接中提取数据非常棘手。

下面是我试图用来调用API然后从XML文件中获取我想要的数据的函数,该链接' https://api.bmreports.com/BMRS/FUELINSTHHCUR/v1?APIKey=&ServiceType=XML'然而,在我努力想要得到任何回报的那一刻,我们才回来

function GetXML(){
        var request = new XMLHttpRequest();
        request.open("GET", 'https://api.bmreports.com/BMRS/FUELINSTHHCUR/v1?APIKey=&ServiceType=XML', false);
        request.send();
        var xml = request.responseXML;
        var fuel = xml.getElementsByTagName("fuelType");
        for(var i = 0; i < users.length; i++) {
            var fuel = fuel[i];
            var fuelnames = user.getElementsByTagName("fuelType");
            for(var j = 0; j < fuelnames.length; j++) {
                alert(fuelnames[j].childNodes[0].nodeValue);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

试试这个例子:

var request = new XMLHttpRequest();
request.open("GET", 'https://api.bmreports.com/BMRS/FUELINSTHHCUR/v1?APIKey=&ServiceType=XML', true);
request.responseType = 'document';
request.overrideMimeType('text/xml');
request.onload = function () {
  if (request.readyState === request.DONE) {
    if (request.status === 200) {
      console.log(request.responseXML);
    }
  }
};
request.send(null);