java - SAXParser Android, ArrayList repeating elements -


i trying process elements within item nodes. focusing on title @ point simplicity, finding when parses, getting same element 3 times.

http://open.live.bbc.co.uk/weather/feeds/en/2643123/3dayforecast.rss

import java.io.inputstream; import java.net.url; import java.util.arraylist;  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 android.util.log;  public class xmlhelper extends defaulthandler {     private string url_main="http://open.live.bbc.co.uk/weather/feeds/en/2643123/3dayforecast.rss";     string tag = "xmlhelper";      boolean currtag = false;     string currtagval = "";           public itemdata item = null;     public arraylist<itemdata> items = new arraylist<itemdata>();      public void get() {         try {             saxparserfactory factory = saxparserfactory.newinstance();             saxparser parser = factory.newsaxparser();             xmlreader reader = parser.getxmlreader();             reader.setcontenthandler(this);             inputstream inputstream = new url(url_main).openstream();             reader.parse(new inputsource(inputstream));         } catch (exception e) {             log.e(tag, "exception: " + e.getmessage());         }     }      // receives notification of start of element      public void startelement(string uri, string localname, string qname,             attributes attributes) throws saxexception {          log.i(tag, "tag: " + localname);          currtag = true;         currtagval = "";         if (localname.equals("channel")) {             item = new itemdata();         }      }      // receives notification of end of element      public void endelement(string uri, string localname, string qname)             throws saxexception {          currtag = false;          if (localname.equalsignorecase("title"))             item.settitle(currtagval);           else if (localname.equalsignorecase("item"))             items.add(item);       }      // receives notification of character data inside element       public void characters(char[] ch, int start, int length)             throws saxexception {          if (currtag) {             currtagval = currtagval + new string(ch, start, length);              currtag = false;         }      } } 

the reason getting same value thrice because creating object when there channel tag in startelement method.

    if (localname.equals("channel")) {         item = new itemdata();     } 

i guess should initiate object whenever there item tag below

    if (localname.equals("item")) { // check item tag         item = new itemdata();     } 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -