需要的JAR
Java SE 1.4以上的版本,下载地址: http://java.sun.com/
开源软件JDOM,下载地址:http://www.jdom.org/
ROME 下载地址 http://wiki.java.net/bin/view/Javawsxml/Rome
1.
Rome
Rome这个开源工具来实现RSS阅读器。Rome支持的格式很多,有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 等等,几乎囊括了目前所有的RSS 和atom版本。最新的Rome版本可以从http://wiki.java.net/bin/view/Javawsxml/Rome上得到。
/*
* Main.java
*
* Created on 2006年8月15日, 下午4:00
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package rssdemo;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Properties;
/**
*
* @author Administrator
*/
public class Main {
String urlStr = "http://rss.sina.com.cn/news/marquee/ddt.xml";
/** Creates a new instance of Main */
public Main() {
}
/**
* 链接rss
*/
public void rss(){
try {
URL feedUrl = new URL(urlStr);
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));
this.show(feed);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (FeedException ex) {
ex.printStackTrace();
}
}
/**
* 通过代理链接rss
*/
public void agentRss() {
try {
URLConnection feedUrl = new URL(urlStr).openConnection();
feedUrl.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (FeedException ex) {
ex.printStackTrace();
}
}
/**
*设置HTTP代理
*/
public void setHttpProxy(){
Properties systemSettings = System.getProperties();
systemSettings.put("http.proxyHost", "192.168.40.80");
systemSettings.put("http.proxyPort", "80");
System.setProperties(systemSettings);
}
/**
*显示信息
*/
public void show(SyndFeed feed){
List list = feed.getEntries();
for (int i=0; i< list.size(); i++) {
SyndEntry entry = (SyndEntry)list.get(i);
System.out.println(entry.getTitle()+" | "+entry.getAuthor()+" | "+entry.getLink());
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main m = new Main();
m.setHttpProxy();
m.rss();
}
}
posted on 2006-08-15 17:46
Derek.Guo 阅读(2006)
评论(0) 编辑 收藏 所属分类:
Java