Android XML Pull Parser - 解析自闭标签后,无法显示标签的属性

时间:2015-06-03 03:59:51

标签: android xmlpullparser

我想从此站点解析xml自闭标签的标签属性: http://api.yr.no/weatherapi/locationforecast/1.9/?lat=-6.1980042;lon=106.8243594

我使用此代码:

主要Activity.java:

public static ArrayList<Datum> parse(String url) throws IOException, XmlPullParserException {
        final ArrayList<Datum> results = new ArrayList<Datum>();

    URL input = new URL(url);

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();

    xpp.setInput(input.openStream(), null);
    int eventType = xpp.getEventType();
    String currentTag = null;
    String temperature = null;

    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {
            currentTag = xpp.getName();
        } else if (eventType == XmlPullParser.TEXT) {
            Log.d("MainActivity", "Masuk Start Tag" );
            if ("temperature".equals(currentTag)) {
                temperature = xpp.getAttributeValue(1);
            }

        } else if (eventType == XmlPullParser.END_TAG) {
            if ("location".equals(xpp.getName())) {
                results.add(new Datum(temperature));
            }
        }
        eventType = xpp.next();
    }
    return results;
}

public static ArrayList<Datum> parse2(String url) throws IOException, XmlPullParserException {
    final ArrayList<Datum> results = new ArrayList<Datum>();

    URL input = new URL(url);

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();

    xpp.setInput(input.openStream(), null);
    xpp.nextTag();
    xpp.require(XmlPullParser.START_TAG, null, "weatherdata");
    while (xpp.nextTag() == XmlPullParser.START_TAG) {

        xpp.require(XmlPullParser.START_TAG, null, "product");
        while (xpp.nextTag() == XmlPullParser.START_TAG) {

            xpp.require(XmlPullParser.START_TAG, null, "time");
            while (xpp.nextTag() == XmlPullParser.START_TAG) {

                xpp.require(XmlPullParser.START_TAG, null, "location");
                xpp.nextTag();
                xpp.require(XmlPullParser.START_TAG, null, "temperature");
                String temperature = xpp.getAttributeValue(1);
                Log.d("MainActivity", "getAttrbuteValue : " + xpp.getAttributeValue(1));


                xpp.nextTag();
                xpp.require(XmlPullParser.END_TAG, null, "location");
                results.add(new Datum(temperature));
            }
            xpp.require(XmlPullParser.END_TAG, null, "time");
        }
        xpp.require(XmlPullParser.END_TAG, null, "product");
    }
    xpp.require(XmlPullParser.END_TAG, null, "weatherdata");

    return results;
}

protected class LoadRecipesTask2 extends AsyncTask<String, Integer, ArrayList<Datum>> {

    @Override
    protected void onPreExecute() {
        mProgressDialog.show();                                                          // 1
    }

    @Override
    protected ArrayList<Datum> doInBackground(String... urls) {
        ArrayList<Datum> datumList = new ArrayList<Datum>();
        for (int i = 0; i < urls.length; i++) {                                          // 2
            try {
                datumList = parse(urls[i]);
                publishProgress((int) (((i+1) / (float) urls.length) * 100));            // 3
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }
        }
        return datumList;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {                                 // 4
        mProgressDialog.setProgress(values[0]);                                          // 5
    }

    @Override
    protected void onPostExecute(ArrayList<Datum> result) {
        mListView.setAdapter(new ArrayAdapter<Datum>(MainActivity.this, R.layout.list_item, result));
        mProgressDialog.dismiss();                                                       // 6
    }
}

Datum类是这样的:

package nl.codestone.recipelist;
public class Datum {

String temperature;

public Datum(String temperature) {
    this.temperature = temperature;

}

public String getTemperature() {
    return temperature;
}

}

我想获取温度的值属性,但值也显示如下: nl.codestone.recipelist.Datum@11a4d5920

有人可以帮忙吗?

0 个答案:

没有答案