rss阅读器 java_RSS阅读器

[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 channelList;

private List newsList;

private NewsDao rssDao;

public RSSService()

{

rssDao = new FileDaoImpl();

}

public List getChannelList()

{

if(channelList == null)

{

channelList = new ArrayList();

//创建“腾讯新闻”频道

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 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 parse(Document doc)

{

List newsList = new ArrayList();

News news = null;

Element root = doc.getRootElement();

//获得item标签

Element eChannel = root.getChild("channel");

List 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;

}

}

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值