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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 310 - (show annotations) (download)
Thu Sep 10 19:09:09 2009 UTC (14 years, 8 months ago) by torben
File size: 3070 byte(s)
Make code work with new servlets (and new db layout)
1 package dk.thoerup.traininfo.provider;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10 import javax.xml.parsers.ParserConfigurationException;
11
12 import org.w3c.dom.Document;
13 import org.w3c.dom.Node;
14 import org.w3c.dom.NodeList;
15 import org.xml.sax.SAXException;
16
17 import android.location.Location;
18 import android.util.Log;
19 import dk.thoerup.traininfo.StationBean;
20 import dk.thoerup.traininfo.util.DownloadUtil;
21
22 public class XmlStationProvider implements StationProvider {
23
24 List<StationBean> stations = new ArrayList<StationBean>();
25
26
27 @Override
28 public List<StationBean> getStations() {
29 return stations;
30 }
31
32 @Override
33 public void lookupStations(Location location) {
34
35 //String url = "http://pumba.t-hoerup.dk:8080/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude();
36 String url = "http://app.t-hoerup.dk/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude();
37 Log.i("url", url);
38 try {
39 stations.clear();
40
41 String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
42
43
44 Document doc = parseXML(xml);
45 Node rootNode = doc.getDocumentElement(); // stations
46 NodeList stationList = rootNode.getChildNodes();
47
48
49 for (int i=0; i<stationList.getLength(); i++) {
50 Node stationNode = stationList.item(i);
51
52 if (! stationNode.getNodeName().equals("station"))
53 continue;
54
55 StationBean station = new StationBean();
56
57 NodeList stationElements = stationNode.getChildNodes();
58 for (int j=0; j<stationElements.getLength(); j++) {
59 Node current = stationElements.item(j);
60
61 String content = null;
62 if (current.getFirstChild() != null)
63 content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value
64
65 String nodeName = current.getNodeName();
66
67 Log.i("XML", "" + nodeName + "=" + content);
68
69 if (nodeName.equals("id"))
70 station.setId( Integer.parseInt(content));
71
72 if (nodeName.equals("name"))
73 station.setName(content);
74
75 if (nodeName.equals("latitude"))
76 station.setLatitude( Double.parseDouble(content) );
77
78 if (nodeName.equals("longitude"))
79 station.setLongitude( Double.parseDouble(content) );
80
81 if (nodeName.equals("stationcode"))
82 station.setCode(content);
83
84 if (nodeName.equals("calcdist"))
85 station.setDistance( (int) Double.parseDouble(content) );
86
87 }
88 stations.add(station);
89 }
90
91
92 } catch (Exception e) {
93 Log.e("XmlStationProvider", "lookupStations: ", e);
94 }
95 }
96
97 public Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException
98 {
99 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
100 return builder.parse( new ByteArrayInputStream(str.getBytes()) );
101 }
102 }

  ViewVC Help
Powered by ViewVC 1.1.20