1

How would I go about fetching multiple XML files? I tried creating an array but that only opens the last file, and as I understand it xmlhttp.open is supposed to cancel any previous send. I tried modifying this which was the closest thing I could find, but my JavaScript knowledge is a bit to limited to adapt it.

This is the basic code I'm using to get one XML file.

if (window.XMLHttpRequest)
 { xmlhttp=new XMLHttpRequest();
}

xmlhttp.open("GET","myfile.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

var x=xmlDoc.getElementsByTagName("TAGNAME");
for (i=0;i<x.length;i++)
{ // Further parsing
}

Also is it possible to then display which file the parsed content comes from in my loop?

Community
  • 1
  • 1
yayheartbeat
  • 445
  • 3
  • 10
  • yes, it came from "myfile.xml" – Jaromanda X Oct 11 '16 at 07:48
  • 1
    `I tried creating an array but that only opens the last file` - how about posting that code, we can then tell you where that is going wrong - and perhaps someone will also show you how you can do this without reorting to (deprecated) synchronous XHR requests! – Jaromanda X Oct 11 '16 at 07:50
  • Don't use async false. Instead add the increment of the array counter and the next call to `xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ parseXml(xmlhttp.responseXM); cnt++; callAjax(); } }` where callAjax() is the name of your function – mplungjan Oct 11 '16 at 07:54

1 Answers1

2

try this:

var arr = ["file1.xml", "file2.xml"],
  cnt = 0, xhr = new XMLHttpRequest(), method = "GET";

function formatXml(file, xmlDoc) {
  var x=xmlDoc.getElementsByTagName("TAGNAME");
  console.log(file,x);
}

function getXml() {
  xhr.open(method, arr[cnt], true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
      formatXml(arr[cnt], xhr.responseText);
      cnt++;
      if (cnt < arr.length) getXml(); // call again
    }
  };
  xhr.send();
}
getXml(); // start it 
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • why wait for the first one to have loaded to load the second one? parallel requests are doable – Kaiido Oct 11 '16 at 08:21
  • I would hesitate to do parallel requests. It can hit a server pretty hard and if there are errors, the requests should be stopped, deferred or handled in some way too. I have not added such code to my example since there is not enough information from the OP to go in that direction. – mplungjan Oct 11 '16 at 08:43
  • Thanks for the help. Tried your solution but it only comes up as empty. No errors, nothing in the console. [Made a mockup example](http://pastebin.com/44imfiLd) of how I tried it. Also tested using my [original code](http://pastebin.com/qnVWdTBK) and that works. – yayheartbeat Oct 11 '16 at 09:39
  • You need to start it: by calling `getXml();` somewhere and you need to feed it a URL that is allowed to call – mplungjan Oct 11 '16 at 09:45
  • Sorry, if I'm a bit slow with this. I tried calling it but that gives me ´TypeError: xmlDoc.getElementsByTagName is not a function. (In 'xmlDoc.getElementsByTagName("bookstore")', 'xmlDoc.getElementsByTagName' is undefined) formatXML row 15, onreadystatechange row 23´ Using the files on my local web server so shouldn't be any access issues. – yayheartbeat Oct 11 '16 at 14:36
  • Sorry change responseText to responseXML – mplungjan Oct 11 '16 at 14:38