0

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]

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

I edited my original post with updated code, your answer doesn't really apply because I can't specify var names = ["a.xml", "b.xml", ...] because I am looping through a variable of unknown size
If you have multiple <File> tags in each <FileNames> tag, you'll need to add a subloop in place of the names.push line: for (var j = 0; j < x[i].getElementsByTagName("File").length; j++) { names.push(x[i].getElementsByTagName("File")[j].childNodes[0].nodeValue); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.