Skip to content

Instantly share code, notes, and snippets.

@robhinds
Created March 19, 2013 20:49
Show Gist options
  • Save robhinds/5200010 to your computer and use it in GitHub Desktop.
Save robhinds/5200010 to your computer and use it in GitHub Desktop.
SAX RSS Parser
public void startElement(String uri, String localName, String qName, Attributes atts) {
chars = new StringBuffer();
}
public void characters(char ch[], int start, int length) {
chars.append(new String(ch, start, length));
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if (localName.equalsIgnoreCase("title")){
currentArticle.setTitle(chars.toString());
} else if (localName.equalsIgnoreCase("description")){
currentArticle.setDescription(chars.toString());
} else if (localName.equalsIgnoreCase("published")){
currentArticle.setPubDate(chars.toString());
} else if (localName.equalsIgnoreCase("id")){
currentArticle.setGuid(chars.toString());
} else if (localName.equalsIgnoreCase("author")){
currentArticle.setAuthor(chars.toString());
} else if (localName.equalsIgnoreCase("content")){
currentArticle.setEncodedContent(chars.toString());
} else if (localName.equalsIgnoreCase("entry")){
}
// Check if looking for article, and if article is complete
if (localName.equalsIgnoreCase("entry")) {
articleList.add(currentArticle);
currentArticle = new Article();
// Lets check if we've hit our limit on number of articles
articlesAdded++;
if (articlesAdded >= ARTICLES_LIMIT){
throw new SAXException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment