Illustration einer tropischen Insel mit Palmen und einem hohen Berg

Java Blog für Programmierer

ROME: Lesen/Schreiben von Atom/RSS-Feeds

· Christian Ullenboom

Rome ist eine Bibliothek zum Lesen/Schreiben/Konvertieren von Atom/RSS-Feeds in den Versionen

  • RSS 0.90
  • RSS 0.91 Netscape
  • RSS 0.91 Userland
  • RSS 0.92/RSS 0.93/RSS 0.94
  • RSS 1.0/RSS 2.0
  • Atom 0.3/Atom 1.0

Ein Beispiel ist schnell programmiert und die API (Doku hier) ist relativ einfach.

package com.sun.syndication.samples; 
import java.io.FileWriter;import java.net.URL;import java.util.List; 
import com.sun.syndication.feed.synd.SyndContent;import com.sun.syndication.feed.synd.SyndEntry;import com.sun.syndication.feed.synd.SyndFeed;import com.sun.syndication.io.SyndFeedInput;import com.sun.syndication.io.XmlReader; 
public class FeedReader{@SuppressWarnings("unchecked")public static void main( String[] args ){try{URL feedUrl = new URL( "http://javainsel.blogspot.com/atom.xml" );SyndFeedInput input = new SyndFeedInput();SyndFeed feed = input.build( new XmlReader( feedUrl ) ); 
StringBuilder sb = new StringBuilder( 1024 );for ( SyndEntry syndFeed : (List )feed.getEntries() ){sb.append( "" + syndFeed.getTitle() + "" );sb.append( "" + ((SyndContent)syndFeed.getContents().get(0)).getValue() + "" );} 
// JFrame f = new JFrame( "Einfache Feed Anzeige" );// f.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );// JEditorPane editor = new JEditorPane( "text/html", sb.toString() );// editor.setEditable( false );// f.add( new JScrollPane(editor) );// f.setSize( 600, 400 );// f.setVisible( true ); 
FileWriter w = new FileWriter( "c:/out.html" );w.write( sb.toString() );w.close();}catch ( Exception e ){e.printStackTrace();}}}

Wenn die JEditorPane besser wäre, könnte man auch (schnell) was sehen. Daher schreibt das Programm eine selbst generierte HTML-Seite ins Dateisystem.

Kategorien: Allgemein

← Alle Beiträge