After suggesting to my AHO class that they consider using RSS feeds in Processing, I realized that there is actually no built-in functionality to do so. The XML class built into Processing is too basic to handle feeds, it seems. To remedy the situation I’ve cooked up a quick hack using the ROME library for RSS / Atom syndication.

To run the code below you’ll need to download ROME and JDOM. Make a “code” subfolder in your sketch and paste “jdom.jar” and “rome-*.jar” into it, then run the code as given. The FeedReader and FeedEntry convenience classes take care of parsing the feed and returning the entries with the most common fields included. Error checking is rudimentary, however.

Code - feedParser.pde

// feedParser.pde
//
// Reads RSS and Atom feeds. Requires ROME
// (https://rome.dev.java.net/)
// and JDOM (http://www.jdom.org/), just make
// a code folder and copy "jdom.jar" and "rome-*.jar"
// into it.
//
// Marius Watz - http://workshop.evolutionzone.com

import com.sun.syndication.feed.synd.*;
import com.sun.syndication.io.*;

FeedReader feed;
String feedurl;

void setup() {
  size(200, 200);

  // load feed
  feedurl="http://feeds.feedburner.com/CodeForm";
  println("Loading feed: "+feedurl);
  feed=new FeedReader(feedurl);

  // print feed data
  println("Feed: "+feed.title);
  println("------------------------------");
  println("Description: "+feed.description);
  println("\nNumber of entries: "+feed.numEntries);
  println("------------------------------");

  // print feed entries
  for(int i=0; i< feed.numEntries; i++) {
    println(i+": "+feed.entry[i]);
  }
}

class FeedReader {
  SyndFeed feed;
  String url,description,title;
  int numEntries;
  FeedEntry entry[];

  public FeedReader(String _url) {
    url=_url;
    try {
      feed=new SyndFeedInput().build(new XmlReader(new URL(url)));
      description=feed.getDescription();
      title=feed.getTitle();

      java.util.List entrl=feed.getEntries();
      Object [] o=entrl.toArray();
      numEntries=o.length;

      entry=new FeedEntry[numEntries];
      for(int i=0; i< numEntries; i++) {
        entry[i]=new FeedEntry((SyndEntryImpl)o[i]);
        println(i+": "+entry[i]);
      }
    }
    catch(Exception e) {
      println("Exception in Feedreader: "+e.toString());
      e.printStackTrace();
    }
  }

}

class FeedEntry {
  Date pubdate;
  SyndEntryImpl entry;
  String author, contents, description, url, title;
  public String newline = System.getProperty("line.separator");

  public FeedEntry(SyndEntryImpl _entry) {
    try {
      entry=_entry;
      author=entry.getAuthor();
      Object [] o=entry.getContents().toArray();
      if(o.length>0) contents=((SyndContentImpl)o[0]).getValue();
      else contents="[No content.]";

      description=entry.getDescription().getValue();
      if(description.charAt(0)==
        System.getProperty("line.separator").charAt(0))
          description=description.substring(1);

      url=entry.getLink();
      title=entry.getTitle();
      pubdate=entry.getPublishedDate();
    }
    catch(Exception e) {
      println("Exception in FeedEntry: "+e.toString());
      e.printStackTrace();
    }

  }

  public String toString() {
    String s;

    s="Title: "+title+newline+
      "URL: "+url+newline+
      "Date: "+pubdate.toString()+newline;

    if(description.length()>50)
      s+="Descr: ["+description.substring(0,50)+
        "...]"+newline;
    else s+="Descr: ["+description+"]"+newline;

    if(contents.length()>50)
      s+="Contents: ["+contents.substring(0,50)+
        "...]"+newline;
    else s+="Contents: ["+contents+"]"+newline;
    return s;
  }
}

There are 7 comments to "Code: Read RSS feeds in Processing". You may leave your own comment.
1. Tyler Weir, September 26th, 2007 at 17:47

Thanks for this, I started looking into ROME for the same reason, but never had time to finish anything of use. This is great, cheers.

2. links for 2007-10-06 « Zero influence, October 6th, 2007 at 02:39

[...] Code & form ยป Code: Read RSS feeds in Processing The XML class built into Processing is too basic to handle feeds, it seems. To remedy the situation I’ve cooked up a quick hack using the ROME library for RSS / Atom syndication. (tags: code processing rss reader) [...]

3. Anthony Starks, December 5th, 2007 at 23:24

This code uses the built-in XML classes to parse OPML -> RSS/Atom feeds

/*
* opmlparse -- parse a OPML file, extracting the Feed URLs,
* open each feed URL, display feed name, and titles
*
* Anthony Starks, ajstarks --at-- gmail.com
*/

import processing.xml.*;

void setup() {
String url = "http://www.toptensources.com//topten/Jeremy-Zawodnys-Top-10/?display=.o pml";
String fu;
String ftitle;
XMLElement titles[];
XMLElement feed;
XMLElement opml = new XMLElement(this, url);
XMLElement[] outline = opml.getChildren(”body/outline”);

for (int i = 0; i < outline.length; i++) {
fu = outline[i].getStringAttribute("xmlUrl");
print(fu);

try {
feed = new XMLElement(this, fu);
println("..Ok");
XMLElement type = feed.getChild(0);
if (type.getName().equals("channel")) { /* RSS */
ftitle = type.getChild("title").getContent();
titles = type.getChildren("item/title");
}
else {
ftitle = feed.getChild("title").getContent();
titles = feed.getChildren("entry/title"); /* Atom */
}
println(ftitle);

for (int nt=0; nt < titles.length; nt++) {
println(" " + titles[nt].getContent());
}
}
catch(Exception e){
println("..Loading Failed");
}
}

}

4. marius watz, December 9th, 2007 at 07:50

Thanks for the code, Anthony. For some reason my code coloring plugin doesn’t work on text from comments, sorry about that.

5. Paul, April 4th, 2008 at 01:53

hey, i can’t compile your code, it seems to be missing something, could u post it again ou make a .zip? thanks in advance,

Paul

6. fkid, April 23rd, 2008 at 22:10

ive got a problem with this line:

for(int i=0; i

inside:

// print feed entries
for(int i=0; i
println(i+”: “+feed.entry[i]);

error: expecting SEMI, found ‘println’

really cant figure out, whats the problem…

thanks anyway

7. marius watz, April 25th, 2008 at 10:21

Sorry, it’s a problem with the code formatting as it appears in Wordpress, still having trouble with it. It’s bizarre how hard it seems to be to get WordPress not to mess with code snippets… I recommend that you download the Code & Form code library 0001, which includes the Feedparser example.

Comment on this entry

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>