RSS reading software.
RSS, short for Really Simple Syndication, is an XML-based file exchange standard and is generally used for quick reading of news titles. In fact, the RSS source is an XML file, so the main part of this RSS reader is to parse the XML document. I used JDOM to parse XML documents. I feel that JDOM is relatively simple.
First, we will introduce the RSS standard. His English document is available here.
Here is an example of an RSS file:
<? XML version = "1.0"?>
& Lt; RSS version = "2.0" & gt;
<Channel>
<Title> liftoff news </title>
<Link> http://liftoff.msfc.nasa.gov/</link>
<Description> liftoff to space registration. </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>
An RSS file is an XML file. The root element is the <RSS> label, which indicates the version of the RSS file. The RSS File above is version 2.0. The reader I wrote only considers version 2.0.
Under the <RSS> label is the <channel> label, which describes the main attributes of this RSS source. <Channel> A tag can contain and multiple sub-tags, four of which are required:
- Title: The channel name. In the previous example, It is liftoff news.
- Link: the URL of the channel, in the previous example http://liftoff.msfc.nasa.gov/
- Description: Description of the channel. In the above example, It is liftoff to space annotation.
- Item: There are many tags that indicate different entries in the channel, such as different news
In addition to the above labels, the channel can also contain many labels. For more information, see the English document. The item tag contains many sub-tags as follows:
- Title: Item Name
- Link: the URL of the item
- Description: Description of the item.
- Author: email address of the author who wrote this item
- Guid: A unique string that identifies this item.
- ..............
For more item labels, refer to the English document.
System Structure
Because I learned to control, I am used to putting my applicationsProgramIt is called a system. With the object-oriented method, the system class diagram is as follows:
The above rsscontent class is used to store the data parsed from the RSS source file. Their definitions can be clearly seen from the figure.
The rssxmlparser class is used to parse data from the RSS source specified by xmlfilename. It uses JDOM to parse XML files.
The code for parsing XML files is as follows:
Static public rsscontent getrsscontent ()
{
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 = 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;
}