I need to access a series of XML documents and am trying to do so with a for loop that generates each request dynamically:
for (i=0;i<routes.length;i++) {
routeRequestURL = "http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=sf-muni&r=" + routes[i].name + "&terse";
routeRequest.open("GET", routeRequestURL);
routeRequest.send();
routeResponse = routeRequest.responseXML;
route = routeResponse.getElementsByTagName("route")[0];
for (var j = 0; j < route.childNodes.length; j++) {
if (route.childNodes[j].tagName == "stop") {
routes[i].stops.push(new Stop(route.childNodes[j].getAttribute("tag"), route.childNodes[j].getAttribute("lat"), route.childNodes[j].getAttribute("lon")));
}
}
}
routes is an array of route objects, which have three variables: name, label, and stops, which is itself an array of stop objects.
I tried out the code in Chrome's javascript console, and it worked when I ran each line within the outer loop with routes[0]. When I tried to run the loop in the console, I got the following error message: TypeError: Cannot call method 'getElementsByTagName' of null.
If running each line of code with routes[0] generates no errors, then why is routeResponse null during the first iteration of the for loop? Am I missing a closure error somewhere?
EDIT: I tried to include a readystatechange callback, but, being new to javascript, couldn't quite figure out how to do it. I tried:
for (i=0;i<routes.length;i++) {
routeRequestURL = "http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=sf-muni&r=" + routes[i].name + "&terse";
routeRequest.open("GET", routeRequestURL);
routeRequest.onreadystatechange = function() {
routeResponse = routeRequest.responseXML;
route = routeResponse.getElementsByTagName("route")[0];
for (var j = 0; j < route.childNodes.length; j++) {
if (route.childNodes[j].tagName == "stop") {
routes[i].stops.push(new Stop(route.childNodes[j].getAttribute("tag"), route.childNodes[j].getAttribute("lat"), route.childNodes[j].getAttribute("lon")));
}
}
}
routeRequest.send();
}
It didn't work.
readystatechangecallback, since the request is asynchronous.