Java ist auch eine Insel

Samstag, Januar 13, 2007

ROME: Lesen/Schreiben von Atom/RSS-Feeds

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<SyndEntry> )feed.getEntries() )
{
sb.append( "<b>" + syndFeed.getTitle() + "</b>" );
sb.append( "<blockquote>" + ((SyndContent)syndFeed.getContents().get(0)).getValue() + "</blockquote>" );
}
// 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.

AddThis Social Bookmark Button

4 Comments:

  • nette sache, aber was hat das mit der JScrollPane auf sich? wieso sollte die besser sein? ist nicht eher die JEditorPane gemeint?

    By Anonymous Anonym, at Januar 14, 2007 10:07 PM  

  • Den Fehler im unteren Absatz (JScrollPane stand da, nun korrekt JEditorPane) habe ich korrigiert. Danke "anonym" für den Kommentar. Ein geläufiger Name übrigens...

    By Blogger Christian Ullenboom, at Januar 14, 2007 11:13 PM  

  • so, nun hab ich einen namen :)
    die JEditorPane ist wirklich etwas alt, bzw. die ganze HTML-Auswertung unter swing ist ja wohl noch auf dem stand von html 3.2...

    By Anonymous suleiman, at Januar 15, 2007 10:55 PM  

  • Das Beispiel funktioniert nicht. Mein Eclipse unter streicht das:

    input.build( new XmlReader( feedUrl ) );

    mit roter Farbe ???

    By Anonymous Bernd, at Mai 03, 2008 9:07 PM  

Kommentar veröffentlichen

<< Home