/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java
ViewVC logotype

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 352 - (hide annotations) (download)
Tue Sep 29 13:35:13 2009 UTC (14 years, 8 months ago) by torben
File size: 2455 byte(s)
First take on timetable parser
1 torben 294 package dk.thoerup.traininfo.provider;
2    
3     import java.util.ArrayList;
4     import java.util.List;
5    
6     import org.w3c.dom.Document;
7     import org.w3c.dom.Node;
8     import org.w3c.dom.NodeList;
9    
10     import android.location.Location;
11     import android.util.Log;
12     import dk.thoerup.traininfo.StationBean;
13     import dk.thoerup.traininfo.util.DownloadUtil;
14 torben 352 import dk.thoerup.traininfo.util.XmlUtil;
15 torben 294
16     public class XmlStationProvider implements StationProvider {
17    
18     List<StationBean> stations = new ArrayList<StationBean>();
19    
20    
21     @Override
22     public List<StationBean> getStations() {
23     return stations;
24     }
25    
26     @Override
27 torben 319 public boolean lookupStations(Location location) {
28     boolean success = false;
29 torben 294 String url = "http://app.t-hoerup.dk/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude();
30     Log.i("url", url);
31     try {
32     stations.clear();
33    
34 torben 302 String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
35 torben 294
36    
37 torben 352 Document doc = XmlUtil.parseXML(xml);
38 torben 294 Node rootNode = doc.getDocumentElement(); // stations
39     NodeList stationList = rootNode.getChildNodes();
40    
41    
42     for (int i=0; i<stationList.getLength(); i++) {
43     Node stationNode = stationList.item(i);
44    
45     if (! stationNode.getNodeName().equals("station"))
46     continue;
47    
48     StationBean station = new StationBean();
49    
50     NodeList stationElements = stationNode.getChildNodes();
51     for (int j=0; j<stationElements.getLength(); j++) {
52     Node current = stationElements.item(j);
53    
54     String content = null;
55     if (current.getFirstChild() != null)
56     content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value
57    
58     String nodeName = current.getNodeName();
59    
60 torben 310 if (nodeName.equals("id"))
61     station.setId( Integer.parseInt(content));
62    
63 torben 294 if (nodeName.equals("name"))
64     station.setName(content);
65    
66     if (nodeName.equals("latitude"))
67     station.setLatitude( Double.parseDouble(content) );
68    
69     if (nodeName.equals("longitude"))
70     station.setLongitude( Double.parseDouble(content) );
71    
72     if (nodeName.equals("stationcode"))
73     station.setCode(content);
74    
75     if (nodeName.equals("calcdist"))
76     station.setDistance( (int) Double.parseDouble(content) );
77    
78     }
79     stations.add(station);
80 torben 319 }
81     success = true;
82 torben 294
83    
84     } catch (Exception e) {
85     Log.e("XmlStationProvider", "lookupStations: ", e);
86     }
87 torben 319 return success;
88 torben 294 }
89     }

  ViewVC Help
Powered by ViewVC 1.1.20