用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

RSS阅读器

2017-06-20 作者: 赶蚊子举报

[java]代码库

package com.ruanko.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import com.ruanko.dao.impl.FileDaoImpl;
import com.ruanko.model.Channel;
import com.ruanko.model.News;
import com.runko.dao.NewsDao;

public class RSSService
{
	private ArrayList<Channel> channelList;
	private List<News> newsList;
	private NewsDao rssDao;
	
	public RSSService()
	{
		rssDao = new FileDaoImpl();
	}
	
	public List<Channel> getChannelList()
	{
		if(channelList == null)
		{
			channelList = new ArrayList<Channel>();
			
			//创建“腾讯新闻”频道
			Channel channel1 = new Channel();
			channel1.setName("新浪国际新闻");
			channel1.setFilePath("NewsFiles/sina.xml");
			channel1.setUrl("http://rss.sina.com.cn/news/world/focus15.xml");
			
			
			//创建“腾讯-国内新闻”频道
			Channel channel2 = new Channel();
			channel2.setName("腾讯国内新闻");
			channel2.setFilePath("NewsFiles/rss_newsgn.xml");
			channel2.setUrl("http://news.qq.com/newsgn/rss_newsgn.xml");
			
			//创建“新浪网-国内要闻”频道
			Channel channel3 = new Channel();
			channel3.setName("新浪体育新闻");
			channel3.setFilePath("NewsFiles/sports.xml");
			channel3.setUrl("http://rss.sina.com.cn/roll/sports/hot_roll.xml");
			
			//创建“新浪网-国际要闻”频道
			Channel channel4 = new Channel();
			channel4.setName("新浪社会新闻");
			channel4.setFilePath("NewsFiles/socials.xml");
			channel4.setUrl("http://rss.sina.com.cn/news/society/focus15.xml");
			
			channelList.add(channel1);
			channelList.add(channel2);
			channelList.add(channel3);
			channelList.add(channel4);
		}
		return channelList;
	}
	
	public List<News> getNewsList(String filePath)
	{
		Document doc = load(filePath);
		newsList = parse(doc);
		return newsList;
	}
	
	private Document load(String filePath)
	{
		Document doc = null;
		SAXBuilder sb = new SAXBuilder(false);
		
		File fXml = new File(filePath);
		if(fXml.exists() && fXml.isFile())
		{
			try
			{
				doc = sb.build(fXml);
			}catch (JDOMException e)
			{
				e.printStackTrace();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		return doc;
	}
	
	private News itemToNews(Element item)
	{
		News news = new News();
		
		//获得节点内容
		String title = item.getChildText("title").trim();
		String link = item.getChildText("link");
		String author = item.getChildText("author");
		String guid = item.getChildText("guid");
		String pubDate = item.getChildText("pubDate");
		String category = item.getChildText("category");
		String description = item.getChildText("description").trim();
		
		//设置节点内容
		news.setTitle(title);
		news.setLink(link);
		news.setAuthor(author);
		news.setGuid(guid);
		news.setPubDate(pubDate);
		news.setCategory(category);
		news.setDescription(description);
		
		return news;
	}
	
	private List<News> parse(Document doc)
	{
		List<News> newsList = new ArrayList<News>();
		News news = null;
		
		Element root = doc.getRootElement();
		
		//获得item标签
		Element eChannel = root.getChild("channel");
		List<Element> itemList = eChannel.getChildren("item");
		
		//生成news对象列表
		for(int i = 0; i < itemList.size(); i++)
		{
			Element item = itemList.get(i);
			news = itemToNews(item);
			newsList.add(news);
		}
		return newsList;
	}
	
	public String newsToString(News news)
	{
		String content  = "";
		content = "标题:"
				+ news.getTitle() + "\r\n"
				+ "链接:"
				+ news.getLink() + "\r\n"
				+ "作者:"
				+ news.getAuthor() + "\r\n"
				+ "发布时间:"
				+ news.getPubDate() + "\r\n"
				+ "-------------------------------------------------------------\n"
				+ news.getDescription() + "\r\n" + "\r\n" + "\r\n";
		return content;
	}
	
	public boolean save()
	{
		boolean flag = false;
		if(rssDao.save(newsList))
		{
			flag = true;
		}
		return flag;
	}
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...