Now a lot of news sites, blog sites have provided RSS subscription function, we can easily RSS subscription, below I use rome-0.9.jar RSS subscription, the specific code is as follows:
To subscribe to webpage news, you must first obtain the URL of the RSS subscription (XML page): http://news.163.com/special/00011k6l/rss_gn.xml;
URL feedurl = new URL ("http://news.163.com/special/00011K6L/rss_gn.xml"); syndfeedinput input = new syndfeedinput (); syndfeed feed = input. build (New xmlreader (feedurl); system. out. println (feed); // obtain entry list = feed from the feed. getentries (); For (INT I = 0; I <list. size (); I ++) {syndentry entry = (syndentry) list. get (I); system. out. println ("link =" + entry. getlink (); system. out. println ("Title =" + entry. gettitle (); system. out. println ("time =" + entry. getpublisheddate (); system. out. println ("content =" + entry. getdescription (). getvalue (); system. out. println ("categories =" + entry. getcategories (). get (0); system. out. println ("========================================== ========================================================== = ");}
It is very convenient to use this package to create an RSS-formatted XML file for subscription. The following is an example:
Package djn. test. RSS. rome; import COM. sun. syndication. feed. RSS. *; import COM. sun. syndication. io. feedexception; import COM. sun. syndication. io. wirefeedoutput; import Java. util. *; public class rssfeedfactory {public static void main (string [] ARGs) {// create a channel object, the corresponding <channel> </channel>/* channel object in RSS has two constructor. One default non-parameter constructor is used to clone the object, * when creating a channel object, you can only use the channel (string type) with the parameter constructor. * This parameter type is very exquisite. At first I filled in some strings and threw them Exception: the illegal type * was forced to rush. After getting the source code of Rome online, I realized that the type was "rss_x.x". * The document of Rome was not specified either, it took a lot of time to study what this type should be. */Channel = new channel ("rss_2.0"); channel. settitle ("test RSS channel's title"); channel. setdescription ("channel description"); channel. setlink ("http://hi.baidu.com/openj/rss"); channel. setencoding ("GBK"); // This list corresponds to the item list items = new arraylist () in RSS; // create an item object, corresponds to <item> </item> item = new item (); item. setauthor ("item author jnduan"); item. settitle ("item title"); // create a description, which is the description part of the item. Description = new description (); description. settype ("item description type"); description. setvalue ("item description value"); item. setdescription (description); items. add (item); channel. setitems (items); // use the wirefeedoutput object to output the RSS text wirefeedoutput out = new wirefeedoutput (); try {system. out. println (Out. outputstring (Channel);} catch (illegalargumentexception e) {e. printstacktrace ();} catch (feedexception e) {e. printstacktrace ();}}}
Generated XML in RSS format
<? XML version = "1.0" encoding = "GBK"?>
<RSS xmlns: content = "http://purl.org/rss/1.0/modules/content/" xmlns: RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns: DC = "http://purl.org/dc/elements/1.1/" xmlns: taxo = "http://purl.org/rss/1.0/modules/taxonomy/" version = "2.0">
<Channel>
<Title> test RSS channel's title </title>
<Link> http://hi.baidu.com/openj/rss </link>
<Description> channel description </description>
<Item>
<Title> item title </title>
<Description> item description value </description>
<Author> item author jnduan </author>
</Item>
</Channel>
</RSS>