/[projects]/android/Side9/src/dk/thoerup/side9/Side9Xml.java
ViewVC logotype

Contents of /android/Side9/src/dk/thoerup/side9/Side9Xml.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 632 - (show annotations) (download)
Tue Mar 23 13:34:52 2010 UTC (14 years, 1 month ago) by torben
File size: 2680 byte(s)
First version
1 package dk.thoerup.side9;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.URL;
8 import java.net.URLConnection;
9
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList;
17 import org.xml.sax.SAXException;
18
19
20
21 public class Side9Xml {
22
23 static final int WIDTH = 100;
24 static final String BASEURL = "http://apps.todic.net/side9/";
25
26 public static Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException
27 {
28 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
29 return builder.parse( new ByteArrayInputStream(str.getBytes()) );
30 }
31
32
33 public static byte[] getContent(String uri) throws IOException {
34 byte buffer[] = new byte[256];
35
36 ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes
37
38 URL url = new URL(uri);
39 URLConnection connection = url.openConnection();
40 InputStream is = connection.getInputStream();
41
42 try {
43 int count;
44 while ( (count = is.read(buffer)) != -1) {
45 baos.write(buffer, 0, count);
46 }
47 } finally {
48 try {
49 is.close();
50 baos.close();
51 } catch (Exception e) {} //never mind if close throws an exception
52 }
53
54 return baos.toByteArray();
55 }
56
57 public Side9Data loadXml() throws IOException, ParserConfigurationException, SAXException{
58 String dataURL = BASEURL + "xml.php?width=" + WIDTH;
59
60 byte data[] = getContent( dataURL );
61 String xmlData = new String(data, "UTF-8");
62 Document doc = parseXML(xmlData);
63
64 Node rootNode = doc.getDocumentElement(); // stations
65 NodeList nodes = rootNode.getChildNodes();
66
67 Side9Data side9Data = new Side9Data();
68
69 for (int i=0; i<nodes.getLength(); i++) {
70 Node currentNode = nodes.item(i);
71
72 String name = currentNode.getNodeName();
73 String content = null;
74
75 if (currentNode.getFirstChild() != null)
76 content = currentNode.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value
77
78
79 if (name.equals("imageurl"))
80 side9Data.url = BASEURL + content;
81 if (name.equals("width"))
82 side9Data.width = Integer.parseInt(content);
83 if (name.equals("height"))
84 side9Data.height = Integer.parseInt(content);
85 if (name.equals("caption"))
86 side9Data.caption = content;
87 }
88
89
90
91
92 return side9Data;
93 }
94
95 }
96

  ViewVC Help
Powered by ViewVC 1.1.20