070924 | Code, Libraries, Processing / Java, Workshops
Tags: Code, feed, pde, processing.org, rome, rss
Tags: Code, feed, pde, processing.org, rome, rss
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;
}
}





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.
[...] 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) [...]
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");
}
}
}
Thanks for the code, Anthony. For some reason my code coloring plugin doesn’t work on text from comments, sorry about that.
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
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
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.
you may want to look at vtd-xml, the latest and most advanced xml processing api
vtd-xml
http://www.livescribe.com/forums/member.php?u=8072&ul23=3 [url=http://www.livescribe.com/forums/member.php?u=8061&ul23=3]buy levitra[/url] hydrocodone online diet pills [url="http://www.livescribe.com/forums/member.php?u=8063&ul23=3"]buy adipex[/url] [LINK http://www.livescribe.com/forums/member.php?u=8067&ul23=3xanax/LINK tmqu
RSS feeds are really great because you are always updated with the latest news or blog posts.*;: