Search code examples
javascripthtmlxmlhttp-live-streamingandroid-livedata

Accessing Elements and Attributes from a Live Feed XML


I am trying to access the time until the next bus arrives for a Live Feed bus transit system in Asheville NC at the given bus stop but I keep returning two console errors: "time is not defined" and "Cannot read property of geElementsbyTagName of undefined"

You can use "470" as an exam stopID to see the XML file.

I have made sure the right stop ID is being added although I am not sure I am adding the ID correctly onto the URL. If the element is nested in another, is that an issue?

var feedURL = "http://webservices.nextbus.com/service/publicXMLFeed?command=predictions&a=art&stopId="+stopID;
var xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function(){
    if(this.readyState == 4 && this.status == 200){
        attribute(this);
    }
};
xhttp.open("GET", feedURL, true);
xhttp.send();

function attribute(feedURL){
    var y; 
    var xmlDoc = xml.responseXML;
    var time = "";
    y = xmlDoc.getElementsbyTagName('prediction');
    time = x.getAttribute('minutes');
}
console.log(time);

I am expecting the number of minutes given until the bus arrives at a given stop.


Solution

  • You are trying to log the value of variable time outside of the function you've defined it in. You could change your code to this and that should work:

    function attribute(feedURL){
        var y; 
        var xmlDoc = xml.responseXML;
        var time = "";
        y = xmlDoc.getElementsbyTagName('prediction');
        time = x.getAttribute('minutes');
    
        console.log(time);
    }