现在的位置: 首页 > 综合 > 正文

用Java+JDOM写的RSS阅读软件

2013年08月19日 ⁄ 综合 ⁄ 共 4179字 ⁄ 字号 评论关闭
RSS阅读软件。
RSS是Really Simple Syndication的缩写,是一种基于XML的文件交换标准,一般用于新闻标题的快速阅读。其实,RSS源就是一个XML文件,所以这个RSS阅读器主要的部分就在于解析XML文档。我用了JDOM解析XML文档,感觉JDOM还是比较简单的。

首先介绍一下RSS标准,他的英文文档可见这里
一下是一个RSS文件的例子:

<?xml version="1.0"?>
<rss version="2.0">
   <channel>
      <title>Liftoff News</title>
      <link>http://liftoff.msfc.nasa.gov/</link>
      <description>Liftoff to Space Exploration.</description>
      <language>en-us</language>
      <item>
         <title>Star City</title>
         <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
         <description>American Life</description>
         <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
         <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
      </item>
      <item>
         <description>Europe Life</description>
         <pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>
         <guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>
      </item>
   </channel>
</rss>


RSS文件是一个XML文件,根元素是<rss>标签,它标明了RSS文件的版本,上面的RSS文件是2.0版本。我写的这个阅读器也只考虑了2.0版本。
在<rss>标签下是<channel>标签,它说明的这个RSS源的主要属性。<channel>标签可以包含和多子标签,其中必须的是4个:

  1. title :channel的名字,上例中为 Liftoff News
  2. link :channel的url地址,上例中为 http://liftoff.msfc.nasa.gov/
  3. description :对channel的描述,上例中为 Liftoff to Space Exploration.
  4. item :这个标签有很多个,表明channel中的不同条目,比如不同的新闻

除了以上的标签,channel还可以包含很多标签。具体的可以参考英文文档。其中item标签又包含很多子标签,如下所示:

  1. title :item的名字
  2. link :item的url地址
  3. description :对item的描述
  4. author :编写这一item的作者的邮件地址
  5. guid :标识这一item的一个唯一的字符串
  6. ..............

更多的item的标签可以参考英文文档。

系统结构
由于是学控制的,所以我习惯于把我的应用程序称为一个系统。用面向对象的方法,系统的类图如下所示:

上面的RSSContent类用于存储从RSS源文件解析出的数据,他们的定义可以从图上清楚地看出。
RssXMLParser类用于从xmlfilename指定的RSS源解析数据,它运用JDOM解析XML文件。

下面给出XML文件解析的代码:
      static public RSSContent getRSSContent()
    {
        RSSContent rsscontent = new RSSContent();
        try{
            SAXBuilder sb = new SAXBuilder();
            Document doc = sb.build(xmlfilename);
            Element root = doc.getRootElement();
            Element Channel = root.getChild("channel");
            Element Child;
            Child = Channel.getChild("title");
            if(Child!=null)
                rsscontent.channel.setTitle(Child.getContent(0).getValue());
            Child = Channel.getChild("language");
            if(Child!=null)
                rsscontent.channel.setLanguage(Child.getContent(0).getValue());
            Child = Channel.getChild("link");
            if(Child!=null)
                rsscontent.channel.setLink(Child.getContent(0).getValue());
            Child = Channel.getChild("description");
            if(Child!=null)
                rsscontent.channel.setDescription(Child.getContent(0).getValue());
            Child = Channel.getChild("pubDate");
            if(Child!=null)
                rsscontent.channel.setPubDate(Child.getContent(0).getValue());
            Child = Channel.getChild("lastBuildDate");
            if(Child!=null)
                rsscontent.channel.setLastBuildDate(Child.getContent(0).getValue());
            Child = Channel.getChild("docs");
            if(Child!=null)
                rsscontent.channel.setDocs(Child.getContent(0).getValue());
            Child = Channel.getChild("generator");
            if(Child!=null)
                rsscontent.channel.setGenerator(Child.getContent(0).getValue());
            Child = Channel.getChild("managingEditor");
            if(Child!=null)
                rsscontent.channel.setManagingEditor(Child.getContent(0).getValue());
            Child = Channel.getChild("webMaster");
            if(Child!=null)
                rsscontent.channel.setWebMaster(Child.getContent(0).getValue());
            Child = Channel.getChild("copyright");
            if(Child!=null)
                rsscontent.channel.setCopyright(Child.getContent(0).getValue());
            List Items = Channel.getChildren("item");
            Element Item;
            for(int i=0;i<Items.size();i++){
                RSSItem rssitem = new RSSItem();
                Item = (Element)Items.get(i);
                Child = Item.getChild("title");
                if(Child!=null)
                    rssitem.title = Child.getContent(0).getValue();
                Child = Item.getChild("link");
                if(Child!=null)
                    rssitem.link = Child.getContent(0).getValue();
                Child = Item.getChild("description");
                if(Child!=null)
                    rssitem.description = Child.getContent(0).getValue();
                rsscontent.channel.item.add(rssitem);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return rsscontent;
    }

【上篇】
【下篇】

抱歉!评论已关闭.