--- android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java 2010/09/16 15:31:31 1065 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java 2010/09/16 15:32:42 1066 @@ -1,19 +1,14 @@ package dk.thoerup.traininfo.provider; -import java.io.StringReader; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; -import org.xml.sax.Attributes; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.helpers.DefaultHandler; +import org.simpleframework.xml.Serializer; +import org.simpleframework.xml.core.Persister; + import android.util.Log; -import dk.thoerup.traininfo.DepartureBean; -import dk.thoerup.traininfo.DepartureEntry; +import dk.thoerup.android.traininfo.common.DepartureBean; +import dk.thoerup.android.traininfo.common.DepartureEntry; import dk.thoerup.traininfo.util.AndroidTimeoutCache; import dk.thoerup.traininfo.util.DownloadUtil; import dk.thoerup.traininfo.util.XmlUtil; @@ -61,19 +56,14 @@ String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival; Log.i("xmlurl",url); String doc = DownloadUtil.getContentString(url, 30000, "ISO-8859-1"); + + Serializer serializer = new Persister(); + + DepartureBean departures = serializer.read(DepartureBean.class, doc); - InputSource source = new InputSource( new StringReader(doc)); - SAXParserFactory spf = SAXParserFactory.newInstance(); - SAXParser sp = spf.newSAXParser(); - XMLReader xr = sp.getXMLReader(); - - DepartureParser departureParser = new DepartureParser(); - xr.setContentHandler(departureParser); - xr.setErrorHandler(departureParser); - xr.parse(source); + return departures; - return departureParser.getDepartures(); } catch (Exception e) { Log.e("XmlDepartureProvider", "looupFunction", e); @@ -81,57 +71,4 @@ } } - - class DepartureParser extends DefaultHandler { - - private DepartureBean departures = new DepartureBean(); - - public DepartureBean getDepartures() { - return departures; - } - - // this can be called several times fore the same text-node if there are many chardata / lines - @Override - public void characters (char ch[], int start, int length) - { - builder.append(ch, start, length); - } - - @Override - public void startElement (String uri, String name, String qName, Attributes atts)throws SAXException - { - if (name.equalsIgnoreCase("train")) - tempDeparture = new DepartureEntry(); - - builder.setLength(0); //reset StringBuilder - } - - @Override - public void endElement (String uri, String name, String qName) throws SAXException - { - if (name.equals("train")) { - departures.entries.add( tempDeparture ); - } else if (name.equals("time")) { - tempDeparture.setTime(builder.toString().trim()); - } else if (name.equals("updated")) { - tempDeparture.setLastUpdate(builder.toString().trim()); - } else if (name.equals("trainnumber")) { - tempDeparture.setTrainNumber(builder.toString().trim()); - } else if (name.equals("destination")) { - tempDeparture.setDestination(builder.toString().trim()); - } else if (name.equals("origin")) { - tempDeparture.setOrigin(builder.toString().trim()); - } else if (name.equals("location")) { - tempDeparture.setLocation(builder.toString().trim()); - } else if (name.equals("status")) { - tempDeparture.setStatus(builder.toString().trim()); - } else if (name.equals("note")) { - tempDeparture.setNote(builder.toString().trim()); - } else if (name.equals("type")) { - tempDeparture.setType(builder.toString().trim()); - } else if (name.equals("notification")) { - departures.notifications.add( builder.toString().trim() ); - } - } - } }