[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;
}
}
by: 发表于:2017-06-21 17:22:32 顶(1) | 踩(1) 回复
??
回复评论