/[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 318 - (hide annotations) (download)
Fri Sep 11 11:51:57 2009 UTC (14 years, 8 months ago) by torben
File size: 2855 byte(s)
Silence chatty debug info
1 torben 294 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://app.t-hoerup.dk/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude();
36     Log.i("url", url);
37     try {
38     stations.clear();
39    
40 torben 302 String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
41 torben 294
42    
43     Document doc = parseXML(xml);
44     Node rootNode = doc.getDocumentElement(); // stations
45     NodeList stationList = rootNode.getChildNodes();
46    
47    
48     for (int i=0; i<stationList.getLength(); i++) {
49     Node stationNode = stationList.item(i);
50    
51     if (! stationNode.getNodeName().equals("station"))
52     continue;
53    
54     StationBean station = new StationBean();
55    
56     NodeList stationElements = stationNode.getChildNodes();
57     for (int j=0; j<stationElements.getLength(); j++) {
58     Node current = stationElements.item(j);
59    
60     String content = null;
61     if (current.getFirstChild() != null)
62     content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value
63    
64     String nodeName = current.getNodeName();
65    
66 torben 310 if (nodeName.equals("id"))
67     station.setId( Integer.parseInt(content));
68    
69 torben 294 if (nodeName.equals("name"))
70     station.setName(content);
71    
72     if (nodeName.equals("latitude"))
73     station.setLatitude( Double.parseDouble(content) );
74    
75     if (nodeName.equals("longitude"))
76     station.setLongitude( Double.parseDouble(content) );
77    
78     if (nodeName.equals("stationcode"))
79     station.setCode(content);
80    
81     if (nodeName.equals("calcdist"))
82     station.setDistance( (int) Double.parseDouble(content) );
83    
84     }
85     stations.add(station);
86     }
87    
88    
89     } catch (Exception e) {
90     Log.e("XmlStationProvider", "lookupStations: ", e);
91     }
92     }
93    
94     public Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException
95     {
96     DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
97     return builder.parse( new ByteArrayInputStream(str.getBytes()) );
98     }
99     }

  ViewVC Help
Powered by ViewVC 1.1.20