Java RSS implementation (column)

Source: Internet
Author: User
Tags netbeans java se

I. RSS Introduction

According to Wikipedia (http://zh.wikipedia.org/wiki/RSS), "RSS is a data exchange specification for sharing news and other Web content", a combination of a series of standards, in XML format. Currently, the most widely used RSS in China is on news websites and blog websites.

Many websites can use RSS readers to personalize their own web pages, such as displaying the latest Sina news, the latest blog articles of their friends, and the latest Google Forum content. In addition, RSS reader can be used for other purposes, such:

Get Weather Forecast

Receive emails. For example, Gmail provides RSS feed.

Get the latest Stock Market

Get music, radio programs, video clips, etc.

 

II. Introduction to Rome

This article uses the open-source tool Rome to implement RSS reader. Rome supports many formats, including RSS 0.90, RSS 0.91 Netscape, RSS 0.91 userland, RSS 0.92, RSS 0.93, RSS 0.94, RSS 1.0, RSS 2.0, Atom 0.3, Atom 1.0, etc, it covers almost all RSS and atom versions. The latest Rome version can be obtained from http://wiki.java.net/bin/view/javawsxml/rome.

 

The RSS reader mainly uses the parsing function of Rome to read the corresponding content from the XML file. I use some simple code to illustrate how to use the classes and methods in Rome.

 

URL feedurl = new URL ("http://rss.sina.com.cn/news/marquee/ddt.xml ");

Syndfeedinput input = new syndfeedinput ();

Syndfeed feed = input. Build (New xmlreader (feedurl ));
 

Table 1: RSS Feed

 

"Http://rss.sina.com.cn/news/marquee/ddt.xml?is an RSS address of Sina news. With three lines of code, you can get an RSS feed object corresponding to this address. This object contains all the RSS content we need. If system. Out. println (feed) is used, the result in table 2 is displayed. The structure of the syndfeed class is clearly displayed.

 

Syndfeedimpl. Contributors = NULL

Syndfeedimpl. Title = News Center-news

Syndfeedimpl. categories [0]. Name =

Syndfeedimpl. categories [0]. taxonomyuri = NULL

Syndfeedimpl. Link = http://news.sina.com.cn/iframe/o/allnews/input/index.htm

Syndfeedimpl. publisheddate = Thu Jun 22 13:20:01 CST 2006

Syndfeedimpl. Entries [0]. updateddate = NULL

Syndfeedimpl. Entries [0]. Contributors = NULL

Syndfeedimpl. Entries [0]. Title = Zawahri in the videotape called on Afghanistan to resist foreign aggression

Syndfeedimpl. Entries [0]. categories [0]. Name =

Syndfeedimpl. Entries [0]. categories [0]. taxonomyuri = NULL

Syndfeedimpl. Entries [0]. Link = http://news.sina.com.cn/w/2006-06-22/11569270955s.shtml

Syndfeedimpl. Entries [0]. publisheddate = Thu Jun 22 11:56:00 CST 2006.

Syndfeedimpl. Entries [0]. Authors = NULL

Syndfeedimpl. Entries [0]. modules [0]. Descriptions = []

Syndfeedimpl. Entries [0]. modules [0]. creators [0] = www. Sina. com. cn

Syndfeedimpl. Entries [0]. modules [0]. Contributors = []

......

Syndfeedimpl. Author = NULL

Syndfeedimpl. Copyright = copyright 1996-2005 Sina Inc. All Rights Reserved
 

Table 2: syndfeed Data Structure

 

The output shows that each news entry is represented by an entry. The following code obtains the entry from the feed:

List list = feed. getentries ();

For (INT I = 0; I <list. Size (); I ++ ){

Syndentry entry = (syndentry) list. Get (I );

}
 

Table 3: Obtain syndentry from syndfeed

 

If the program is behind the firewall, add some proxy settings in the program. For example, the following HTTP proxy is used:

Properties systemsettings = system. getproperties ();

Systemsettings. Put ("HTTP. proxyhost", "myproxyserver.com ");

Systemsettings. Put ("HTTP. proxyport", "80 ");

System. setproperties (systemsettings );
 

Table 4: proxy settings

Sometimes, you may encounter an error message "Java. Io. ioexception: server returned HTTP response code: 403 for URL. Generally, the security settings of the server do not accept access from the Java program as the client. The solution is to set the user agent of the client. The sample code is as follows:

Urlconnection feedurl = new jurl (urlstr). openconnection ();

Feedurl. setrequestproperty ("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; digext )");

Syndfeedinput input = new syndfeedinput ();

Syndfeed feed = input. Build (New xmlreader (feedurl ));
 

Table 5: Set User-Agent

 

Rome provides many functions. In addition to parsing feeds, it can also generate feeds. Use Rome to create an RSS feed for your website content, so that others can learn about the updates of your website content at any time through the RSS reader.

 

Using Rome requires two conditions:

Java SE 1.4 or later,: http://java.sun.com/

Open source software JDOM,: http://www.jdom.org/

 

3. Use netbeans to quickly develop a simple instance

The following describes how to quickly use Rome and netbeans to build a web-based RSS reader using a simple prototype development procedure.

 

Netbeans is an open source Java ide software: http://www.netbeans.org. Netbeans is used because it has built-in tomcat, which can save a lot of configuration and running time. It has powerful functions and can efficiently develop various applications such as Java SE, Java EE, and Java me.

 

Use netbeans 5.0 to create a web project.

Open netbeans, select "file-> New Project", in the new project window, select "Web" for "category", select "Web application" for the project, and click "Next ". In the "new web application" window, enter the project name, such as "webrssreader" and project location, and click "finish ".

Figure 1: create a web project for netbeans

 

Add two jar files to the newly created webrssreader project:

JDOM. jar: JDOM open source project (http://www.jdom.org /)

Rome. jar: Rome open source project (http://wiki.java.net/bin/view/Javawsxml/Rome)

Figure 2: Add a library file

 

Add code to index. jsp

<% @ Page contenttype = "text/html" %>

<% @ Page pageencoding = "UTF-8" %>

 

 

<HTML>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">

<Title> Sina news </title>

</Head>

<Body>

<%

Java. util. properties systemsettings = system. getproperties ();

Systemsettings. Put ("HTTP. proxyhost", "mywebcache.com ");

Systemsettings. Put ("HTTP. proxyport", "8080 ");

System. setproperties (systemsettings );

 

 

String urlstr = "http://rss.sina.com.cn/news/marquee/ddt.xml ";

Java.net. urlconnection feedurl = new java.net. URL (urlstr). openconnection ();

Feedurl. setrequestproperty ("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; digext )");

Com. Sun. syndication. Io. syndfeedinput input = new COM. Sun. syndication. Io. syndfeedinput ();

Com. Sun. syndication. Feed. synd. syndfeed feed = input. Build (new COM. Sun. syndication. Io. xmlreader (feedurl ));

%>

<Div align = "center">

<H1> <% = feed. gettitle () %>

<Table border = 1 cellpadding = 3 width = "700">

<Tr>

<TH> Number </Th>

<TH> title </Th>

<TH> time </Th>

</Tr>

<%

Java. util. List list = feed. getentries ();

For (INT I = 0; I <list. Size (); I ++ ){

Com. Sun. syndication. Feed. synd. syndentry entry = (COM. Sun. syndication. Feed. synd. syndentry) list. Get (I );

%>

<Tr>

<TD> <% = I + 1%> </TD>

<TD> <a href = "<% = entry. getlink () %>"> <% = entry. gettitle () %> </a> </TD>

<TD> <% = entry. getpublisheddate () %> </TD>

</Tr>

<% }%>

</Table>

</Div>

<Br>

</Body>

</Html>
 

Table 6: all source code of index. jsp

 

Run the project. Right-click the webrssreader project and choose run project ".

Figure 3: running a program

 

The running result is as follows.

Figure 4: program running result

Iv. Summary

RSS is an application technology of Web2.0. Web2.0 advocates personalization and participation. In the development of this simple instance, some open-source software is used. Open-source software is a kind of embodiment of the spirit of "everyone for me, everyone for me. Many times, we don't need to develop some tools from the ground up. standing on the shoulders of our predecessors, developing better applications may be something programmers should consider more.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.