Matt
Matt

Reputation: 2930

xmlhttp from variable rather than url - javascript

I am pulling in multiple xml files that are stored in the var names from a loop, but I want to use the xmlhttp request with the variable names, since it changes each iteration rather than reassigning the path every time i.e. folder/file.xml. So Basically I need help using the xmlhttp request to pull in xml details with a variable rather than a direct link i.e.:

I have:

xmlhttp.open("GET","FileNames.xml",false);

But I need:

var names = "xmlf/file.xml";
xmlhttp.open("GET",names,false);

EDIT:

xmlhttp.open("GET","FileNames.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("FileNames");
document.write("<tr>");
for (i=0;i<x.length;i++)
  { 

  document.write("<td>");
  var names = (x[i].getElementsByTagName("File")[0].childNodes[0].nodeValue);
 // document.write(names);

my var names changes with each loop iteration, how can I store it like var names = [names[i],names[i], to x.length]

Upvotes: 0

Views: 139

Answers (1)

feedbackloop
feedbackloop

Reputation: 491

If I have this right, you can do the following:

var names = [];
for (i=0;i<x.length;i++)
{
    document.write("<td>");
    names.push(x[i].getElementsByTagName("File")[0].childNodes[0].nodeValue);
    // your remaining code here
}

Note names.push, which lets you add an element to the names array.

Now, with all the names stored, you can loop through and do your XML requests on them:

for (var n = 0; n < names.length; n++) {
    xmlhttp.open("GET", names[n], false);
    xmlhttp.send();
    var xmlDoc = xmlhttp.responseXML;
    // act on XML response here
}

Upvotes: 1

Related Questions