RSS发布(生成XML)和读取URL

用到的JAR:

rome-0.9; jdom.jar

 

发布RSS Feed:

生成xml文件, 生成的文件放置于 项目WEBContent中,直接访问.

xml生成代码:

import java.io.FileWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.SyndFeedOutput;

public class RssWrite {

	public static void main(String[] args) throws Exception {
		DateFormat dateParser = new SimpleDateFormat("yyyyMMddhhmmss");
		
		SyndFeed feed = new SyndFeedImpl();
		feed.setFeedType("rss_2.0");
		feed.setTitle("Sample Feed (created with Rome)");
        feed.setLink("http://rome.dev.java.net");
        feed.setDescription("This feed has been created using Rome (Java syndication utilities");
        
        List<SyndEntry> entries = new ArrayList<SyndEntry>();
        SyndEntry entry;
        SyndContent description;
        
        //item 内容
        //重复add产生多个item
        entry = new SyndEntryImpl();
        entry.setTitle("Rome v1.0");
        entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome01");
        entry.setPublishedDate(new Date());
        description = new SyndContentImpl();
        description.setType("text/plain");
        description.setValue("hello caohaigang");
        entry.setDescription(description);
        entries.add(entry);
        
        feed.setEntries(entries);
        
        String fileName = "c:\\rss\\feed"+dateParser.format(new Date())+".xml";
        Writer writer = new FileWriter(fileName);
        SyndFeedOutput output = new SyndFeedOutput();
        output.output(feed,writer);
        writer.close();

        System.out.println("The feed has been written to the file ["+fileName+"]");
	}
}

 

网上Rss Feed读取代码:

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.fetcher.FeedFetcher;
import com.sun.syndication.fetcher.impl.FeedFetcherCache;
import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;
import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher;

public class RssReader {

	public static void main(String[] args) throws Exception {
		FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
		FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache);

		URL url = new URL("http://feed.feedsky.com/wolfit");
		//url是XML文件, 如http://feed.feedsky.com/xxx.xml
		SyndFeed feed = feedFetcher.retrieveFeed(url);
		List entryList = feed.getEntries();
		for (int i = 0; i < entryList.size(); i++) {
			SyndEntry entry = (SyndEntry) entryList.get(i);
			System.out.println("Published Date: " + entry.getPublishedDate());
			System.out.println("Title: " + entry.getTitle());
			System.out.println("Link: " + entry.getLink());
			SyndContent sc = entry.getDescription();
			System.out.println("Description: " + sc.getValue());
			System.out.println("------------------------------");
		}
	}
}

 

你可能感兴趣的:(java,c,xml,.net,sun)