pullParser Android提供了logcat中的所有项目,但没有在应用程序中

时间:2014-06-16 20:22:08

标签: android xmlpullparser

我在logcat中解析的xml文件的输出显示了所有项目,但我的活动只显示了最后一项。

logcat的:

06-16 22:06:19.780   9871-10111/notendop.norge I/System.out﹕ TIME: 23:00:00
06-16 22:06:19.780   9871-10111/notendop.norge I/System.out﹕ DATE: 2014-06-16
06-16 22:06:19.780   9871-10111/notendop.norge I/System.out﹕ regen VALUE 0
06-16 22:06:19.780   9871-10111/notendop.norge I/System.out﹕ wind richting Nord-nordøst
06-16 22:06:19.785   9871-10111/notendop.norge I/System.out﹕ wind Svak vind
06-16 22:06:19.785   9871-10111/notendop.norge I/System.out﹕ temp 12
06-16 22:06:19.785   9871-10111/notendop.norge I/System.out﹕ TIME: 00:00:00
06-16 22:06:19.785   9871-10111/notendop.norge I/System.out﹕ DATE: 2014-06-17
06-16 22:06:19.785   9871-10111/notendop.norge I/System.out﹕ regen VALUE 0
06-16 22:06:19.785   9871-10111/notendop.norge I/System.out﹕ wind richting Øst-nordøst
06-16 22:06:19.785   9871-10111/notendop.norge I/System.out﹕ wind Lett bris

(部分)代码:

 private ArrayList<NewsItem> parseNews(InputStream in) throws XmlPullParserException, IOException {

    ArrayList<NewsItem> newsList = new ArrayList<NewsItem>();
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    XmlPullParser pullParser = factory.newPullParser();
    pullParser.setInput(in, "UTF-8");

    int eventType = pullParser.getEventType();

    NewsItem item = null;

    while (eventType != XmlPullParser.END_DOCUMENT) {String tagName;
         if (eventType == XmlPullParser.START_TAG)
         {tagName = pullParser.getName();
         if (tagName.equals(TAG_TABULAR)) {item = new NewsItem();}

         else if (tagName.equals(TAG_TIME)) {
             if (item != null) {
                 item.mTime_SV = pullParser.getAttributeValue(null, "from");
                 StringTokenizer tokens = new StringTokenizer(item.mTime_SV, "T");
                 item.mDate = tokens.nextToken();
                 item.mTime = tokens.nextToken();
                 System.out.println("TIME: " + item.mTime);
                 System.out.println("DATE: " + item.mDate);
             }
         }

         else if (tagName.equals(TAG_TEMP)) {
            if (item != null) {
                item.mTemp_SV = pullParser.getAttributeValue(null,"value");
                System.out.println("temp " + item.mTemp_SV);
            }
         }

         else if (tagName.equals(TAG_REGEN_MIN)) {
             if (item != null) {
                 item.mRegen_min_SV = pullParser.getAttributeValue(null,"value");
                 System.out.println("regen VALUE " + item.mRegen_min_SV);
             }
         }

         else if (tagName.equals(TAG_WIND_DESC)) {
            if (item != null) {
                item.mWind_desc_SV = pullParser.getAttributeValue(null,"name");
                System.out.println("wind " + item.mWind_desc_SV);
            }
         }

        else if (tagName.equals(TAG_WIND_RICHTING)) {
            if (item != null) {
                item.mWind_richting_SV = pullParser.getAttributeValue(null,"name");
                System.out.println("wind richting " + item.mWind_richting_SV);
            }
        }

          //   eventType = pullParser.next();
    }

    else if (eventType == XmlPullParser.END_TAG) { tagName = pullParser.getName();
        if (tagName.equals(TAG_TABULAR)) {
            newsList.add(item);
            item = null;
        }
    }

    eventType = pullParser.next();
    }

    return newsList;

     }

我错过了一个循环或其他东西吗?

1 个答案:

答案 0 :(得分:0)

每次点击标记TAG_TABULAR时都会这样做

tagName.equals(TAG_TABULAR)) {item = new NewsItem();

所以你名单中的任何内容都会被删除。

也许你应该更好地格式化你的代码,你可能已经抓住了这个