//Goes through character by character when parsing whats inside of a tag. @Override public void characters(char[] ch, int start, int length) throws SAXException { if (currentItem != null) { //If parsingTitle is true, then that means we are inside a <title> tag so the text is the title of an item. if (parsingTitle) currentItem.setTitle(new String(ch, start, length)); //If parsingLink is true, then that means we are inside a <link> tag so the text is the link of an item. else if (parsingLink) currentItem.setLink(new String(ch, start, length)); //If parsingDescription is true, then that means we are inside a <description> tag so the text is the description of an item. else if (parsingDescription) currentItem.setDescription(new String(ch, start, length)); else if (parsingPubDate) currentItem.setPubDate(new String(ch, start, length)); } }
public static List<RssItem> parse(String rssFeed, Activity activity) { try { Thread.sleep(6000); } catch (InterruptedException e1) { e1.printStackTrace(); } List<RssItem> list = new ArrayList<RssItem>(); XmlPullParser parser = Xml.newPullParser(); InputStream stream = null; try { stream = new URL(rssFeed).openStream(); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { // auto-detect the encoding from the stream parser.setInput(stream, null); int eventType = parser.getEventType(); boolean done = false; RssItem item = null; while (eventType != XmlPullParser.END_DOCUMENT && !done) { String name = null; switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: name = parser.getName(); if (name.equalsIgnoreCase(ITEM)) { Log.i("new item", "Create new item"); item = new RssItem(); } else if (item != null) { if (name.equalsIgnoreCase(LINK)) { Log.i("Attribute", "setLink"); item.setLink(parser.nextText()); } else if (name.equalsIgnoreCase(DESCRIPTION)) { Log.i("Attribute", "description"); item.setDescription(parser.nextText().trim()); } else if (name.equalsIgnoreCase(PUB_DATE)) { Log.i("Attribute", "date"); item.setPubDate(parser.nextText()); } else if (name.equalsIgnoreCase(TITLE)) { Log.i("Attribute", "title"); item.setTitle(parser.nextText().trim()); } } break; case XmlPullParser.END_TAG: name = parser.getName(); Log.i("End tag", name); if (name.equalsIgnoreCase(ITEM) && item != null) { Log.i("Added", item.toString()); list.add(item); } else if (name.equalsIgnoreCase(CHANNEL)) { done = true; } break; } eventType = parser.next(); } } catch (Exception e) { throw new RuntimeException(e); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } return list; }