Created
November 22, 2011 20:53
-
-
Save tonycosentini/1386924 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RssHandler extends DefaultHandler { | |
private Stack<String> mTagStack = new Stack<String>(); | |
private String currentTagContents; | |
private String currentTag() { return mTagStack.peek(); } | |
private String parentTag() { return mTagStack.get(mTagStack.size() - 2); } | |
public ArrayList<RssItem> mItems = new ArrayList<RssItem>(); | |
private RssItem mCurrentItem; | |
@Override | |
public void startElement (String uri, String localName, String qName, Attributes attributes) { | |
currentTagContents = ""; | |
mTagStack.add(qName); | |
System.out.println("Current tag: " + currentTag()); | |
if (currentTag().equals("item")) | |
mCurrentItem = new RssItem(); | |
} | |
@Override | |
public void endElement (String uri, String localName, String qName) { | |
if (currentTag().equals("item")) | |
mItems.add(mCurrentItem); | |
else if (currentTag().equals("title") && parentTag().equals("item")) | |
mCurrentItem.title = currentTagContents; | |
else if (currentTag().equals("description") && parentTag().equals("item")) | |
mCurrentItem.body = currentTagContents; | |
else if (currentTag().equals("link") && parentTag().equals("item")) | |
mCurrentItem.permalinkURL = currentTagContents; | |
else if (currentTag().equals("author") && parentTag().equals("item")) | |
mCurrentItem.author = currentTagContents; | |
else if (currentTag().equals("pubDate")) { | |
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz"); | |
try { mCurrentItem.publishedTime = formatter.parse(currentTagContents); } catch (Exception e) {} | |
} | |
mTagStack.pop(); | |
} | |
@Override | |
public void characters (char[] ch, int start, int length) { | |
currentTagContents += new String(get(ch, start, length)); | |
} | |
// Helpers for dealing with character arrays returned in characters method. | |
public static char[] get(char[] array, int offset) { | |
return get(array, offset, array.length - offset); | |
} | |
public static char[] get(char[] array, int offset, int length) { | |
char[] result = new char[length]; | |
System.arraycopy(array, offset, result, 0, length); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment