I'm developing an Android app that fetches an XML from a RSS feed and parses it. Some entries are empty, so I have to exclude those. The issue is that comparing things like result == "" or result.length() == 0 are not working. I tried appending a .toString() to the end of the result, but it doesn't work too. Hours of Googling and no solution. Does anyone know what I'm doing wrong? It must be something foolish as I'm quite new to Android development.

Thanks!

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    XmlPullParser parser = factory.newPullParser();

    parser.setInput(new StringReader(XML));
    int eventType = parser.getEventType();

    News news = new News();
    String tagName = "";

    newsList = new ArrayList<News>();

    while (eventType != XmlPullParser.END_DOCUMENT){

        if(eventType == XmlPullParser.START_TAG) {
              if(parser.getName().contains("item")){
                  news = new News();
            }
                  tagName = parser.getName();

        } else if(eventType == XmlPullParser.END_TAG) {
              if(parser.getName().contains("item")){
                  if(news.pubDate.length() > 0){
                      newsList.add(news);
                      Log.e("TITLE", news.title);
                  }
              }
          } else if(eventType == XmlPullParser.TEXT) {
              if(tagName.contains("description")){
                  news.description = parser.getText();
            }
              else if(tagName.contains("title")){
                  news.title = parser.getText();
              }
              else if(tagName.contains("link")){
                  news.link = parser.getText();
              }
              else if(tagName.contains("pubDate")){
                  news.pubDate = parser.getText();
              }
            }
          eventType = parser.next();
         }
有帮助吗?

解决方案

The solution was to add a trim() to the string I'm checking the length. Apparently, XmlPullParser will often pass the right values once and then pass an empty value. So it's also important to check if the value is empty before processing it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top