package dk.thoerup.side9; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import android.util.Log; public class Side9Xml { static final int WIDTH = 450; static final String BASEURL = "http://apps.todic.net/side9/"; public static Document parseXML(byte data[]) throws SAXException, IOException, ParserConfigurationException { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return builder.parse( new ByteArrayInputStream( data) ); } public static byte[] getContent(String uri) throws IOException { byte buffer[] = new byte[4096]; ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes URL url = new URL(uri); URLConnection connection = url.openConnection(); connection.setConnectTimeout(2500); InputStream is = connection.getInputStream(); try { int count; while ( (count = is.read(buffer)) != -1) { baos.write(buffer, 0, count); } } finally { try { is.close(); baos.close(); } catch (Exception e) {} //never mind if close throws an exception } return baos.toByteArray(); } public static Side9Data loadXml() throws IOException, ParserConfigurationException, SAXException{ String dataURL = BASEURL + "xml.php?width=" + WIDTH; byte data[] = getContent( dataURL ); Document doc = parseXML( data ); Node rootNode = doc.getDocumentElement(); // stations NodeList nodes = rootNode.getChildNodes(); Side9Data side9Data = new Side9Data(); for (int i=0; i