XML feed question (irishrail)

edited April 2018 in Questions about Code

Hi there.

I am working with an XML feed of train stations that shows departure/arrival times. The feed was retreived from this page: api.irishrail.ie/realtime/index.htm?realtime_irishrail

The feed in question is this one: api.irishrail.ie/realtime/realtime.asmx/getStationDataByCodeXML?StationCode=mhide

Other feeds within the first link list all info for trains and stations to be contained within one XML, but the above feed does not allow you to view all (200 or so) stations at once. Instead it requires you to type your station into the URL/address bar each time. So basically, the 'mhide' at the end of the above URL is one station code. The literature says that this is the only way to access the information:Screen Shot 2018-04-22 at 18.41.49

Does anyone know a method that would enable me to access all stations at once?

Many thanks in advance :)

Answers

  • I (kind of) solved it using the following code, though I'm currently troubleshooting the reason that this particular code runs very slowly.

    String urlStationInfo = "http://" + "api.irishrail.ie/realtime/realtime.asmx/getStationsFilterXML?StationText=";
    
    Arc[] arcs;
    
    XML xmlA; 
    XML xmlS; 
    
    XML[] childrenA;
    XML[] childrenS;
    IntList arrayDueTimes;
    IntList arrayLateTimes;
    StringList arrayID;
    StringList stationID;
    
    int angle=0; 
    int scaler = 10;
    
    Timer timer = new Timer(10000);
    
    
    //________________________________________________________________________________________________________________________________ 
    void setup() {
      size(800, 800, P2D);  
      requestStationData();
      requestArrayData();
      arcs = new Arc[arrayDueTimes.size()];
    
      for (int i = 0; i < arrayDueTimes.size(); i++) {
        arcs[i] = new Arc(360/arrayDueTimes.size(), i);
      }
      timer.start();
    } 
    
    
    //________________________________________________________________________________________________________________________________ 
    void draw() {
      background(255,  193,  37);
    
      if (timer.isFinished()) {   // Every one second, make a new request.
        println("Timer is finished: Requesting values and restarting"); 
        requestStationData();
        requestArrayData();   
        timer.start();  // And restart the timer. 
      }
    
      for (Arc a : arcs) {
        a.display();
      }
    }
    
    //________________________________________________________________________________________________________________________________ 
    class Arc {
      float radian;
      int noOfArcs;
    
      Arc(float tempRadian, int tempNoOfArcs) {
        radian = tempRadian;
        noOfArcs = tempNoOfArcs;
      }
    
      void display() {   
        float startAngle = 0;  
        float lastAngle = 0;  
        for (int i=0; i<arrayDueTimes.size(); i++) {
    
          fill(255,0,0);
          stroke(0);
          strokeWeight(0);
          arc(width/2, height/2, scaler*abs(arrayDueTimes.get(i))+arrayLateTimes.get(i), scaler*abs(arrayDueTimes.get(i))+arrayLateTimes.get(i), lastAngle, lastAngle+radians(radian));
    
          fill(0+i*1);
          stroke(0);
          strokeWeight(0);
          arc(width/2, height/2, scaler*abs(arrayDueTimes.get(i)), scaler*abs(arrayDueTimes.get(i)), lastAngle, lastAngle+radians(radian));
          lastAngle += radians(radian);
          startAngle = startAngle + lastAngle;
        }
      }
    }  
    
    
    //________________________________________________________________________________________________________________________________
    class Timer {
      int savedTime;
      boolean running = false;
      int totalTime;
    
      Timer(int tempTotalTime) {
        totalTime = tempTotalTime;
      }
    
      void start() {
        running = true;
        savedTime = millis();
      }
    
      boolean isFinished() {
        int passedTime = millis() - savedTime;
        if (running && passedTime > totalTime) {
          running = false;
          return true;
        } else {
          return false;
        }
      }
    }
    
    
    //________________________________________________________________________________________________________________________________ 
    void requestStationData() {   
      xmlS = loadXML(urlStationInfo);  
      childrenS = xmlS.getChildren("objStationFilter");   
      stationID = new StringList();
    
      for (int i = 0; i < childrenS.length; i++) {
        XML sIDElement = childrenS[i].getChild("StationCode"); 
        String sID = sIDElement.getContent();  
        stationID.append(sID);   
      }   
    } 
    
    
    //________________________________________________________________________________________________________________________________ 
    void requestArrayData() {   
      arrayID = new StringList();
      arrayDueTimes = new IntList();
      arrayLateTimes = new IntList();
    
      //xmlA = loadXML("stationData.xml");
      for (int i = 0; i < childrenS.length; i++) {    
        String myString = stationID.get(i);
        xmlA = loadXML("http://api.irishrail.ie/realtime/realtime.asmx/getStationDataByCodeXML?StationCode=" + myString);  
    
        childrenA = xmlA.getChildren("objStationData");  
    
        for (int j = 0; j < childrenA.length; j++) {   
          //println(childrenA.length);
          XML originElement = childrenA[j].getChild("Origin");
          String origin = originElement.getContent();      
    
          XML destinationElement = childrenA[j].getChild("Destination");
          String destination = destinationElement.getContent();   
    
          XML dueElement = childrenA[j].getChild("Duein");
          int due = dueElement.getIntContent();     
          arrayDueTimes.append(due);
    
          XML fullNameElement= childrenA[j].getChild("Stationfullname");
          String fullName = fullNameElement.getContent();   
          arrayID.append(fullName);
    
          XML lateElement = childrenA[j].getChild("Late");
          int late = lateElement.getIntContent();     
          arrayLateTimes.append(late);
    
          println(" Station: " + fullName + ". Next Train: " + origin + " to " + destination + " due in " + due + "mins / Running " + late + " mins late." );
        } 
      } 
    }
    
  • Ok. But there was no need to start a new thread for the slowness question. Keep it all together so we have context.

  • edited April 2018

    Okay, sorry koogs. I shall continue in this one.

    So I adjusted my class according to the advice from TfGuy44 and revisiting some of Daniel Schiffman's arrays/objects tutorials. This is where I'm at. I'm confused about where to loop: should I loop inside the display of the class or in draw()? :(

    String urlStationInfo = "http://" + "api.irishrail.ie/realtime/realtime.asmx/getStationsFilterXML?StationText=";
    
    Arc[] arcs;
    
    XML xmlA; 
    XML xmlS; 
    
    XML[] childrenA;
    XML[] childrenS;
    
    FloatList arrayDueTimes;
    IntList arrayLateTimes;
    StringList arrayID;
    StringList stationID;
    
    int angle=0; 
    int scaler = 10;
    
    Timer timer = new Timer(10000);
    
    
    //________________________________________________________________________________________________________________________________ 
    void setup() {
      size(800, 800, P2D);  
      requestStationData();
      requestArrayData();
      arcs = new Arc[arrayDueTimes.size()];
    
      for (int i = 0; i < arrayDueTimes.size(); i++) {
        arcs[i] = new Arc(360.0/arrayDueTimes.size(), i);
      }
    
      timer.start();
    } 
    
    
    //________________________________________________________________________________________________________________________________ 
    void draw() {
      background(255,  193,  37);
      if (timer.isFinished()) {   // Every one second, make a new request.
        println("Timer is finished: Requesting values and restarting"); 
        requestStationData();
        requestArrayData();   
        timer.start();  // And restart the timer. 
      }
    
      for (int i = 0; i < arrayDueTimes.size(); i ++ ) {
        arcs[i].display(i);
      }
    }
    
    
    //________________________________________________________________________________________________________________________________ 
    class Arc {
      float radian;
      int noOfArcs;
    
      Arc(float tempRadian, int tempNoOfArcs) {
        radian = tempRadian;
        noOfArcs = tempNoOfArcs;
      }
    
      void display(int  i) {   
        float startAngle = 0;  
        float lastAngle = 0;  
          fill(0);
          stroke(0);
          strokeWeight(1);
          arc(width/2, height/2, scaler*abs(arrayDueTimes.get(i)), scaler*abs(arrayDueTimes.get(i)), lastAngle, lastAngle+radians(radian));
          lastAngle += radians(radian);
          startAngle = startAngle + lastAngle;
      }
    }
    
    
    //________________________________________________________________________________________________________________________________ 
    void requestStationData() {   
      xmlS = loadXML(urlStationInfo);  
      childrenS = xmlS.getChildren("objStationFilter");   
      stationID = new StringList();
    
      for (int i = 0; i < childrenS.length; i++) {
        XML sIDElement = childrenS[i].getChild("StationCode"); 
        String sID = sIDElement.getContent();  
        stationID.append(sID);   
      }   
    } 
    
    
        //________________________________________________________________________________________________________________________________
        class Timer {
          int savedTime;
          boolean running = false;
          int totalTime;
    
          Timer(int tempTotalTime) {
            totalTime = tempTotalTime;
          }
    
          void start() {
            running = true;
            savedTime = millis();
          }
    
          boolean isFinished() {
            int passedTime = millis() - savedTime;
            if (running && passedTime > totalTime) {
              running = false;
              return true;
            } else {
              return false;
            }
          }
        }
    
    
            //________________________________________________________________________________________________________________________________ 
    void requestArrayData() {   
      arrayID = new StringList();
      arrayDueTimes = new FloatList();
      arrayLateTimes = new IntList();
    
      //xmlA = loadXML("stationData.xml");
      for (int i = 0; i < childrenS.length; i++) {    
        String myString = stationID.get(i);
          //xmlA = loadXML("stationData.xml");
        xmlA = loadXML("http://api.irishrail.ie/realtime/realtime.asmx/getStationDataByCodeXML?StationCode=" + myString);  
    
        childrenA = xmlA.getChildren("objStationData");  
    
        for (int j = 0; j < childrenA.length; j++) {   
          //println(childrenA.length);
          XML originElement = childrenA[j].getChild("Origin");
          String origin = originElement.getContent();      
    
          XML destinationElement = childrenA[j].getChild("Destination");
          String destination = destinationElement.getContent();   
    
          XML dueElement = childrenA[j].getChild("Duein");
          int due = dueElement.getIntContent();     
          arrayDueTimes.append(due);
    
          XML fullNameElement= childrenA[j].getChild("Stationfullname");
          String fullName = fullNameElement.getContent();   
          arrayID.append(fullName);
    
          XML lateElement = childrenA[j].getChild("Late");
          int late = lateElement.getIntContent();     
          arrayLateTimes.append(late);
    
          //println(" Station: " + fullName + ". Next Train: " + origin + " to " + destination + " is due in " + due + "minutes.");
        } 
      } 
    }
    
  • In case this is of use... before I tried to put the arcs into a class, it worked fine with the following code:

    String urlStationInfo = "http://" + "api.irishrail.ie/realtime/realtime.asmx/getStationsFilterXML?StationText=";
    
    XML xmlA; 
    XML xmlS; 
    
    XML[] childrenA;
    XML[] childrenS;
    
    FloatList arrayDueTimes;
    IntList arrayLateTimes;
    StringList arrayID;
    StringList stationID;
    
    int angle=0; 
    int scaler = 10;
    
    Timer timer = new Timer(10000);
    
    
    //________________________________________________________________________________________________________________________________ 
    void setup() {
      size(800, 800, P2D);  
      requestStationData();
      requestArrayData();  
      timer.start();
    } 
    
    
    //________________________________________________________________________________________________________________________________ 
    void draw() {
      background(255,  193,  37);
      if (timer.isFinished()) {   // Every one second, make a new request.
        println("Timer is finished: Requesting values and restarting"); 
        requestStationData();
        requestArrayData();   
        timer.start();  // And restart the timer. 
      }
      drawArcs();
    }
    
    
    //________________________________________________________________________________________________________________________________ 
    void drawArcs(){  
      float startAngle = 0;  
      float lastAngle = 0;
      float radian = 360.0/arrayDueTimes.size();
    
      for (int i = 0; i < arrayDueTimes.size(); i++) {    
        fill(255);
        stroke(0);
        strokeWeight(0);
        arc(width/2, height/2, scaler*(arrayDueTimes.get(i)), scaler*(arrayDueTimes.get(i)), lastAngle, lastAngle+radians(radian));
        lastAngle += radians(radian);
        startAngle = startAngle + lastAngle;
      }//for 
    }
    
    
    //________________________________________________________________________________________________________________________________ 
    void requestStationData() {   
      xmlS = loadXML(urlStationInfo);  
      childrenS = xmlS.getChildren("objStationFilter");   
      stationID = new StringList();
    
      for (int i = 0; i < childrenS.length; i++) {
        XML sIDElement = childrenS[i].getChild("StationCode"); 
        String sID = sIDElement.getContent();  
        stationID.append(sID);   
      }   
    } 
    
    
    //________________________________________________________________________________________________________________________________ 
    void requestArrayData() {   
      arrayID = new StringList();
      arrayDueTimes = new FloatList();
      arrayLateTimes = new IntList();
    
      //xmlA = loadXML("stationData.xml");
      for (int i = 0; i < childrenS.length; i++) {    
        String myString = stationID.get(i);
          //xmlA = loadXML("stationData.xml");
        xmlA = loadXML("http://api.irishrail.ie/realtime/realtime.asmx/getStationDataByCodeXML?StationCode=" + myString);  
    
        childrenA = xmlA.getChildren("objStationData");  
    
        for (int j = 0; j < childrenA.length; j++) {   
          //println(childrenA.length);
          XML originElement = childrenA[j].getChild("Origin");
          String origin = originElement.getContent();      
    
          XML destinationElement = childrenA[j].getChild("Destination");
          String destination = destinationElement.getContent();   
    
          XML dueElement = childrenA[j].getChild("Duein");
          int due = dueElement.getIntContent();     
          arrayDueTimes.append(due);
    
          XML fullNameElement= childrenA[j].getChild("Stationfullname");
          String fullName = fullNameElement.getContent();   
          arrayID.append(fullName);
    
          XML lateElement = childrenA[j].getChild("Late");
          int late = lateElement.getIntContent();     
          arrayLateTimes.append(late);
    
          println(" Station: " + fullName + ". Next Train: " + origin + " to " + destination + " is due in " + due + "minutes.");
        } 
      } 
    }
    
    
    //________________________________________________________________________________________________________________________________
    class Timer {
      int savedTime;
      boolean running = false;
      int totalTime;
    
      Timer(int tempTotalTime) {
        totalTime = tempTotalTime;
      }
    
      void start() {
        running = true;
        savedTime = millis();
      }
    
      boolean isFinished() {
        int passedTime = millis() - savedTime;
        if (running && passedTime > totalTime) {
          running = false;
          return true;
        } else {
              return false;
            }
      }
    }
    
  • The class should be responsible for drawing itself. If it contains one thing then it should draw one thing and the iterating over the items, if appropriate, should be at the level that the group is defined.

  • Thanks for that, I kind of get you. I'll omit the class for the time being until I get a better understanding. My issue was not beginning with a class, but instead trying to make a class when the code was quite large.

Sign In or Register to comment.