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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 649 - (hide annotations) (download)
Mon Apr 19 12:49:31 2010 UTC (14 years, 1 month ago) by torben
File size: 2903 byte(s)
Code sync (use image width=400)
1 torben 632 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 torben 633 import android.util.Log;
20 torben 632
21    
22 torben 633
23 torben 632 public class Side9Xml {
24    
25 torben 649 static final int WIDTH = 400;
26 torben 632 static final String BASEURL = "http://apps.todic.net/side9/";
27    
28     public static Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException
29     {
30     DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
31     return builder.parse( new ByteArrayInputStream(str.getBytes()) );
32     }
33    
34    
35     public static byte[] getContent(String uri) throws IOException {
36     byte buffer[] = new byte[256];
37    
38     ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes
39    
40     URL url = new URL(uri);
41     URLConnection connection = url.openConnection();
42     InputStream is = connection.getInputStream();
43    
44     try {
45     int count;
46     while ( (count = is.read(buffer)) != -1) {
47     baos.write(buffer, 0, count);
48     }
49     } finally {
50     try {
51     is.close();
52     baos.close();
53     } catch (Exception e) {} //never mind if close throws an exception
54     }
55    
56     return baos.toByteArray();
57     }
58    
59 torben 634 public static Side9Data loadXml() throws IOException, ParserConfigurationException, SAXException{
60 torben 632 String dataURL = BASEURL + "xml.php?width=" + WIDTH;
61    
62     byte data[] = getContent( dataURL );
63 torben 649 String xmlData = new String(data, "ISO-8859-1");
64 torben 632 Document doc = parseXML(xmlData);
65    
66     Node rootNode = doc.getDocumentElement(); // stations
67     NodeList nodes = rootNode.getChildNodes();
68    
69     Side9Data side9Data = new Side9Data();
70    
71     for (int i=0; i<nodes.getLength(); i++) {
72     Node currentNode = nodes.item(i);
73    
74     String name = currentNode.getNodeName();
75     String content = null;
76    
77     if (currentNode.getFirstChild() != null)
78     content = currentNode.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value
79    
80    
81     if (name.equals("imageurl"))
82     side9Data.url = BASEURL + content;
83     if (name.equals("width"))
84     side9Data.width = Integer.parseInt(content);
85     if (name.equals("height"))
86     side9Data.height = Integer.parseInt(content);
87     if (name.equals("caption"))
88     side9Data.caption = content;
89     }
90 torben 633
91     Log.i("Side9Xml", "url: " + side9Data.url);
92     Log.i("Side9Xml", "size: " + side9Data.width + "/" + side9Data.height);
93     Log.i("Side9Xml", "capt: " + side9Data.caption);
94    
95 torben 632
96    
97    
98    
99     return side9Data;
100     }
101    
102     }
103    

  ViewVC Help
Powered by ViewVC 1.1.20