最近做了一个rss作者库,顺便也看看了使用rome开源解析rss xml的文章,并使用一下demo代码做一个简单的验证,
官网提供的包地址如下rome-1.0.jar
官网地址rome
demo代码如下
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
public class RssFeed{
public boolean parseRssXml(){
boolean result = true;
try {
URL feedUrl = new URL("http://rss.sina.com.cn/news/marquee/ddt.xml");
URLConnection urlConn = feedUrl.openConnection();
//feedUrl.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
SyndFeedInput input = new SyndFeedInput();
XmlReader reader = new XmlReader(urlConn);
SyndFeed feed = input.build(reader);
int entriesSize = feed.getEntries().size();
System.out.println("News:" + entriesSize + " FeedType:"+ feed.getFeedType() + " Title:" + feed.getTitle());
SyndEntry entry;
SyndContent description;
for (int i = 0; i < entriesSize; i++) {
entry = new SyndEntryImpl();
entry = (SyndEntry) (feed.getEntries()).get(i);
description = new SyndContentImpl();
description = entry.getDescription();
String title = entry.getTitle();//文章题目
String link = entry.getLink();//文章链接
String author = entry.getAuthor();//作者
String desContent = description.getValue();//文章描述
Date publishDate = entry.getPublishedDate();//文章发布日期
System.out.print((i + 1) + ". " + title.trim()+ " 作者:" + author);
System.out.println(publishDate != null ? " 日期:"+ publishDate : "");
System.out.println(desContent.trim());
System.out.println(link);
}
} catch (Exception ex) {
ex.printStackTrace();
result = false;
}
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
RssFeed rssFeed = new RssFeed();
rssFeed.parseRssXml();
}
}