Android Open Source - Weather Xml Parser






From Project

Back to project page Weather.

License

The source code is released under:

Apache License

If you think the Android project Weather listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.volitic.weather;
//w  w  w  .  ja v  a2s.co m
import android.os.AsyncTask;
import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;

public class XmlParser {

    // We don't use namespaces
    private static final String ns = null;

    Parser receiver;

    // XML Tags
    public static final String CURRENT = "current";
    public static final String CITY = "city";
    public static final String COORD = "coord";
    public static final String COUNTRY  = "country";
    public static final String SUN = "sun";
    public static final String TEMPERATURE = "temperature";
    public static final String HUMIDITY = "humidity";
    public static final String PRESSURE = "pressure";
    public static final String WIND = "wind";
    public static final String SPEED = "speed";
    public static final String DIRECTION = "direction";
    public static final String CLOUDS = "clouds";
    public static final String PRECIPITATION = "precipitation";
    public static final String WEATHER = "weather";
    public static final String LASTUPDATE = "lastupdate";

    public XmlParser(Parser receiver) {
        this.receiver = receiver;
    }

    public interface Parser {
        public void parseFailed();
        public void parseSucceeded(Weather weather);
    }

    public void parse( InputStream stream ){
        new ParseTask().execute(stream);
    }

    public Weather parseData(InputStream in) throws IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            return readFeed(parser);
        } catch (XmlPullParserException exception){
            // malformed xml
            return null;
        } finally {
            in.close();
        }
    }


    private Weather readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {

        // this implementation effectively flattens the xml -- this should be fixed

        Weather current_conditions = new Weather();
        parser.require(XmlPullParser.START_TAG, ns, CURRENT);

        // while there are more tags left
        while (parser.getDepth() > 0){

            if (parser.getEventType() != XmlPullParser.START_TAG) {
                parser.next();
                continue;
            }
            String name = parser.getName();
            // Starts by looking for the entry tag
            if ( name.equals(CITY) ) {
                current_conditions.set(Weather.text.LOCATION,
                        parser.getAttributeValue(ns, "name") );

            } else if( name.equals(COORD) ) {
                current_conditions.set(Weather.number.LATITUDE,
                        Float.parseFloat(parser.getAttributeValue(ns, "lat") ));
                current_conditions.set(Weather.number.LONGITUDE,
                        Float.parseFloat(parser.getAttributeValue(ns, "lon")));

                //} else if( name.equals(COUNTRY) ) {

            } else if( name.equals(SUN) ) {
                current_conditions.set(Weather.number.SUNRISE,
                        Time.dateToSeconds(parser.getAttributeValue(ns, "rise"), true));
                current_conditions.set(Weather.number.SUNSET,
                        Time.dateToSeconds(parser.getAttributeValue(ns, "set"), true));

            } else if( name.equals(TEMPERATURE) ) {
                current_conditions.set(Weather.number.TEMPERATURE,
                        Float.parseFloat(parser.getAttributeValue(ns, "value") ));
                current_conditions.set(Weather.number.TEMPERATURE_HIGH,
                        Float.parseFloat(parser.getAttributeValue(ns, "max") ));
                current_conditions.set(Weather.number.TEMPERATURE_LOW,
                        Float.parseFloat(parser.getAttributeValue(ns, "min") ));

                String units = parser.getAttributeValue(ns, "unit");

                if(units.equals("celsius")){
                    current_conditions.setUnits(Weather.unit_system.METRIC);
                } else if ( units.equals("fahrenheit")){
                    current_conditions.setUnits(Weather.unit_system.IMPERIAL);
                } else {
                    current_conditions.setUnits(Weather.unit_system.SI);
                }

            } else if( name.equals(HUMIDITY) ) {
                current_conditions.set(Weather.number.HUMIDITY,
                        Float.parseFloat(parser.getAttributeValue(ns, "value")));
            } else if( name.equals(PRESSURE) ) {
                current_conditions.set(Weather.number.PRESSURE,
                        Float.parseFloat(parser.getAttributeValue(ns, "value")));

                //} else if( name.equals(WIND) ) {

            } else if( name.equals(SPEED) ) {
                current_conditions.set(Weather.number.WIND_SPEED,
                        Float.parseFloat(parser.getAttributeValue(ns, "value")));

            } else if( name.equals(DIRECTION) ) {
                current_conditions.set(Weather.number.WIND_DIRECTION,
                        Float.parseFloat(parser.getAttributeValue(ns, "value")));
                current_conditions.set(Weather.text.WIND_BEARING,
                        parser.getAttributeValue(ns, "code"));

            } else if( name.equals(CLOUDS) ) {
                current_conditions.set(Weather.number.CLOUD_COVER,
                        Float.parseFloat(parser.getAttributeValue(ns, "value")));

            } else if( name.equals(PRECIPITATION) ) {
                String mode = parser.getAttributeValue(ns, "mode");

                current_conditions.set(Weather.text.PRECIPITATION_TYPE, mode );

                if(!mode.equals("no")){
                    current_conditions.set(Weather.number.PRECIPITATION,
                            Float.parseFloat(parser.getAttributeValue(ns, "value")) /3 ); // rate comes per 3 hours. change to p/h
                } else {

                    current_conditions.set(Weather.number.PRECIPITATION, 0.0f);
                }

            } else if( name.equals(WEATHER) ) {
                current_conditions.set(Weather.text.QUALITATIVE_SUMMARY,
                        parser.getAttributeValue(ns,"value"));

                //} else if( name.equals(LASTUPDATE) ){

            }

            //advance to next
            parser.next();

        }

        //parsing complete
        return current_conditions;
    }


    private class ParseTask extends AsyncTask<InputStream, Void, Weather> {
        @Override
        protected Weather doInBackground(InputStream... streams) {

            try {
                // paramater comes from the execute() call. params[0] is the url.
                return  parseData(streams[0]);
            } catch (IOException e) {
                return null;
            }
        }

        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(Weather result) {
            if(result == null ){
                receiver.parseFailed();
            } else {
                receiver.parseSucceeded(result);
            }
        }
    }

}




Java Source Code List

com.volitic.weather.AboutDialog.java
com.volitic.weather.AutoCompleteCursorAdapter.java
com.volitic.weather.AutoCompletePreference.java
com.volitic.weather.DatabaseHelper.java
com.volitic.weather.MainActivity.java
com.volitic.weather.Network.java
com.volitic.weather.SettingsActivity.java
com.volitic.weather.Time.java
com.volitic.weather.Weather.java
com.volitic.weather.XmlParser.java