[Java] RSS阅读器 →→→→→进入此内容的聊天室

来自 , 2020-02-22, 写在 Java, 查看 174 次.
URL http://www.code666.cn/view/b2dd1403
  1. package com.ruanko.service;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import org.jdom2.Document;
  9. import org.jdom2.Element;
  10. import org.jdom2.JDOMException;
  11. import org.jdom2.input.SAXBuilder;
  12.  
  13. import com.ruanko.dao.impl.FileDaoImpl;
  14. import com.ruanko.model.Channel;
  15. import com.ruanko.model.News;
  16. import com.runko.dao.NewsDao;
  17.  
  18. public class RSSService
  19. {
  20.         private ArrayList<Channel> channelList;
  21.         private List<News> newsList;
  22.         private NewsDao rssDao;
  23.        
  24.         public RSSService()
  25.         {
  26.                 rssDao = new FileDaoImpl();
  27.         }
  28.        
  29.         public List<Channel> getChannelList()
  30.         {
  31.                 if(channelList == null)
  32.                 {
  33.                         channelList = new ArrayList<Channel>();
  34.                        
  35.                         //创建“腾讯新闻”频道
  36.                         Channel channel1 = new Channel();
  37.                         channel1.setName("新浪国际新闻");
  38.                         channel1.setFilePath("NewsFiles/sina.xml");
  39.                         channel1.setUrl("http://rss.sina.com.cn/news/world/focus15.xml");
  40.                        
  41.                        
  42.                         //创建“腾讯-国内新闻”频道
  43.                         Channel channel2 = new Channel();
  44.                         channel2.setName("腾讯国内新闻");
  45.                         channel2.setFilePath("NewsFiles/rss_newsgn.xml");
  46.                         channel2.setUrl("http://news.qq.com/newsgn/rss_newsgn.xml");
  47.                        
  48.                         //创建“新浪网-国内要闻”频道
  49.                         Channel channel3 = new Channel();
  50.                         channel3.setName("新浪体育新闻");
  51.                         channel3.setFilePath("NewsFiles/sports.xml");
  52.                         channel3.setUrl("http://rss.sina.com.cn/roll/sports/hot_roll.xml");
  53.                        
  54.                         //创建“新浪网-国际要闻”频道
  55.                         Channel channel4 = new Channel();
  56.                         channel4.setName("新浪社会新闻");
  57.                         channel4.setFilePath("NewsFiles/socials.xml");
  58.                         channel4.setUrl("http://rss.sina.com.cn/news/society/focus15.xml");
  59.                        
  60.                         channelList.add(channel1);
  61.                         channelList.add(channel2);
  62.                         channelList.add(channel3);
  63.                         channelList.add(channel4);
  64.                 }
  65.                 return channelList;
  66.         }
  67.        
  68.         public List<News> getNewsList(String filePath)
  69.         {
  70.                 Document doc = load(filePath);
  71.                 newsList = parse(doc);
  72.                 return newsList;
  73.         }
  74.        
  75.         private Document load(String filePath)
  76.         {
  77.                 Document doc = null;
  78.                 SAXBuilder sb = new SAXBuilder(false);
  79.                
  80.                 File fXml = new File(filePath);
  81.                 if(fXml.exists() && fXml.isFile())
  82.                 {
  83.                         try
  84.                         {
  85.                                 doc = sb.build(fXml);
  86.                         }catch (JDOMException e)
  87.                         {
  88.                                 e.printStackTrace();
  89.                         } catch (IOException e)
  90.                         {
  91.                                 e.printStackTrace();
  92.                         }
  93.                 }
  94.                 return doc;
  95.         }
  96.        
  97.         private News itemToNews(Element item)
  98.         {
  99.                 News news = new News();
  100.                
  101.                 //获得节点内容
  102.                 String title = item.getChildText("title").trim();
  103.                 String link = item.getChildText("link");
  104.                 String author = item.getChildText("author");
  105.                 String guid = item.getChildText("guid");
  106.                 String pubDate = item.getChildText("pubDate");
  107.                 String category = item.getChildText("category");
  108.                 String description = item.getChildText("description").trim();
  109.                
  110.                 //设置节点内容
  111.                 news.setTitle(title);
  112.                 news.setLink(link);
  113.                 news.setAuthor(author);
  114.                 news.setGuid(guid);
  115.                 news.setPubDate(pubDate);
  116.                 news.setCategory(category);
  117.                 news.setDescription(description);
  118.                
  119.                 return news;
  120.         }
  121.        
  122.         private List<News> parse(Document doc)
  123.         {
  124.                 List<News> newsList = new ArrayList<News>();
  125.                 News news = null;
  126.                
  127.                 Element root = doc.getRootElement();
  128.                
  129.                 //获得item标签
  130.                 Element eChannel = root.getChild("channel");
  131.                 List<Element> itemList = eChannel.getChildren("item");
  132.                
  133.                 //生成news对象列表
  134.                 for(int i = 0; i < itemList.size(); i++)
  135.                 {
  136.                         Element item = itemList.get(i);
  137.                         news = itemToNews(item);
  138.                         newsList.add(news);
  139.                 }
  140.                 return newsList;
  141.         }
  142.        
  143.         public String newsToString(News news)
  144.         {
  145.                 String content  = "";
  146.                 content = "标题:"
  147.                                 + news.getTitle() + "\r\n"
  148.                                 + "链接:"
  149.                                 + news.getLink() + "\r\n"
  150.                                 + "作者:"
  151.                                 + news.getAuthor() + "\r\n"
  152.                                 + "发布时间:"
  153.                                 + news.getPubDate() + "\r\n"
  154.                                 + "-------------------------------------------------------------\n"
  155.                                 + news.getDescription() + "\r\n" + "\r\n" + "\r\n";
  156.                 return content;
  157.         }
  158.        
  159.         public boolean save()
  160.         {
  161.                 boolean flag = false;
  162.                 if(rssDao.save(newsList))
  163.                 {
  164.                         flag = true;
  165.                 }
  166.                 return flag;
  167.         }
  168. }
  169.  

回复 "RSS阅读器"

这儿你可以回复上面这条便签

captcha