Use RSS and atom for news Union

Source: Internet
Author: User

The emergence of RSS and atom technology has brought a new era for news syndication. In any case, it takes time for the web site administrator to manually publish news and manage Email users every day. This article explains how to use RSS and atom combined formats to implement a general news publishing architecture, make the publishing process easier, and minimize human errors.

RSS and atom combination

RSS and atom are similar XML-based document formats that describe the list of related information called feeds. These summaries consist of multiple items with a set of extensible additional metadata; for example, each item has a title. These summaries are mainly used for Web content Federation, such as weblog or news title for websites and directly for user proxies.

Two examples of RSS 2.0 and atom 1.0

List1. RSS 2.0 summary example

 
       
       
        
         feed title  
         http://yourwebsite.com/ 
        
          feed description 
         
        
          en-US 
         
        
          Mon, 03 Jan 2005 12:00:00 GMT 
         
         
          Article Title  
          http://yourwebsite.com/articlelink.html  
         
           your content was ded here. 
          
         
         sports  
         http://yourwebsite.com/sportslink.html  
        
          your content has ded here. 
          
        
       

List 2. Atom 1.0 feed example

<? XML version = "1.0" encoding = "UTF-8"?> <Feed xmlns = "http://www.w3.org/2005/Atom"> <title> feed title </title> <link href = "http://yourwebsite.com/"/> <updated> 2003-12-13t18: 30: 02z </updated> <author> <Name> your name </Name> </author> <ID> urn: UUID: 60a76c80-d399-11d9-b93c-0003939e0af6 </ID> <entry> <title> Article Title </title> <link href = "http://yourwebsite.com/articlelink.html"/> <ID> urn: UUID: 1225c695-cfb8-4ebb-aaaa-80da344efa6a </ID> <updated> 2003-12-13t18: 30: 02z </updated> <summary> some text. </Summary> </entry> <title> sports </title> <link href = "http://yourwebsite.com/sportslink.html"/> <ID> urn: UUID: 1225c695-cfb8-4ebb-aaaa-80da344e45ab90 </ID> <updated> 2003-12-14t13: 30: 55z </updated> <summary> some text. </Summary> </entry> </feed>

Similarity Between RSS and atom Abstract

From the previous two examples (listing 1 and Listing 2), we can see that RSS and atom have similar XML-based formats. They have the same basic structure and only have a difference in the node expression.

Each summary file actually represents a channel. It contains the channel title, Link, description, author, and so on. The channel information provides basic information about the summary. The channel information is followed by some items. Each item represents a real news orArticle. Generally, each item contains the title, Link, Update Time, and summary information.

Differences between RSS and atom Abstract

Refer to RSS 2.0 and atom 1.0 and compared to review the differences between RSS and atom.

Table 1. Comparison of RSS 2.0 and atom 1.0

Differences RSS 2.0 Atom 1.0
Deployment RSS 2.0 is widely deployed. Atom 1.0 has not been widely deployed yet.
Specifications Harvard University has copyright rights and has frozen the RSS 2.0 specification. The atompub Working Group (IETF) agreed on the atom 1.0 specification and may be revised in the future.
Required content RSS 2.0 contains the title, link, and description of the desired abstract level. It does not need any separate item fields in the abstract. Atom 1.0 contains the title (which can be blank), unique identifier, and last updated timestamp required for the summary and entries.
Payload) RSS 2.0 can contain plain text or escape HTML, but it cannot tell which of the two is provided. Atom 1.0 contains a payload container.
All or part of content RSS 2.0 has<Description>It can contain all the text or outline of the entry. It has no built-in method to identify whether the content is complete. Atom 1.0 provides separate<Summary>And<Content> element. If it is non-text or non-local content, the summary will be useful for accessibility reasons.
Automatic Discovery RSS 2.0 uses different methods for automatic discovery. Atom 1.0 standard automatic discovery.
Extraction and aggregation RSS 2.0 has only one recognizable form: One<RSS>Documentation. Atom 1.0 allows independent atom entry documents that can be transmitted over any network protocol, for example, XMPP. Atom also supports aggregation of summaries, where entries point to the summaries they come from, provided that they will be included in other summaries.

General architecture of RSS and atom Publishing Systems

With the development and improvement of RSS and atom format specifications, more and more web applications can be implemented.Program. The most common and typical Implementation of RSS or atom is the news publishing system.

The following is a general architecture that uses RSS and atom abstract to implement the publishing system. This architecture consists of three parts:

    • Summary generator Subsystem
    • Summary running Subsystem
    • Automatic release Subsystem

Now let's take a look at the details of these three subsystems.

Abstract Generator

The abstract generator is responsible for generating XML-based summary files. The class diagram of the digest generator is as follows:

Figure 1. Summary generator class diagram

The core of the digest generator isFeedfilemanagerClass. It generates XML files related to a specific abstract. The abstract has two types:AtomfeedAndRssfeed. Both feed classes contain a feed channel class and a feed Item class. The implementation of the feed channel class and feed Item class varies according to their different structures.

Summary running Subsystem

The Digest running subsystem provides several digest running functions to run these two types of digest files. For example,Insertitem (),Deleteitem (),Updateitem ()Function. The class diagram of the running subsystem is as follows:

Figure 2. Class diagram of the summary running Subsystem

First, an abstract factory model is required.PublisherfactoryGenerate one of the two abstract publishers. Then the publisher passesDatacollectionClass extracts data from the database.

Automatic release Subsystem

The Auto Release subsystem is a timer for updating summary files at a fixed time. It uses the first two subsystems and enables the publishing system to run.

Figure 3. Category diagram of the automatic release Subsystem

Step-by-step implementation of automatic generation of RSS and atom summaries

Now let's take a look at the detailed implementation process of each step. First, figure 4 provides a chart for the general implementation process. It shows several major building blocks and their relationships.

Figure 4. Implementation Process Overview

The entire application is a large timer, so you can set a fixed start time in it. When the time reaches, the timer starts to do the scheduled work-to generate the summary file. First, the application generates an XML file, then forms an RSS and atom abstract, and finally writes these summaries to the created XML file. The source of the abstract can be databases, files, or various other resources.

Timer

Timer is the core of the publishing system. It triggers the system to generate a summary file at a specified time or at a fixed time every day. Java standard scheduling classes can be used for Java applications:TimerAndTimertaskTo implement a repetitive scheduling task.

More generally, the scheduling cycle tasks are combined in the publishing system. For details about how to combine and schedule cyclic tasks, See Schedule repeated tasks in Java applications.

Listing 3. Example of the timer start Method

Public void start () {schedulertask ST = new schedulertask () {public void run () {try {updatenewsfeeds (); // concrete method of update feed files. updatebooksfeeds ();...} catch (exception e) {e. printstacktrace ();}}};

Generate an XML-based Summary File

RSS and atom summaries are based on XML, so it is important to generate these XML-based files during implementation. In the digest generator subsystemFeedfilemanagerClass is responsible for this task.

Figure 5. feedfilemanager class

Normally, this class should includeCreatefeedfile ()Method. This method requires three things:

    • Create an XML file. To achieve this goal, you can create a static method createxmlfile ().
    • Interacts with the summary running subsystem to create related channels and item information.
    • Write all the information back to the XML file.

Listing 4. createfeedfile method example

Public String createfeedfile (string channelid, string name, string type) throws exception {... createxmlfile (file, type); ipublishable publisher = publisherfactory. createpublisher (type, file); feed = getfeed (type); channel = feed. getchannel (); arraylist itemlist = feed. getitemlist (); Publisher. insertchannel (Channel); Publisher. insertitemlist (itemlist); Publisher. writeback (File); Return file ;}

Form RSS/atom Abstract

In the previous process, the publishing factory created a publisher and inserted related channel information and item lists into a summary.

Listing 5. Examples of inserted channels and item listsCode

Publisher. insertchannel (Channel); Publisher. insertitemlist (itemlist );

To form a precise channel and item list, We need to extract data from the database and input the data to the abstract mode. The following demonstrates how to retrieve data and input information to the relevant databean:

Listing 6. Sample Code of the summary

... Newsbeanmanager newsmanager = new newsbeanmanager (); newslist = newsmanager. getallnews (); // interact with database... for (iterator it = newslist. iterator (); it. hasnext (); it. next () {Title = "news" + I + ":" + (newsbean) newslist. get (I )). gettitle (); link = (newsbean) newslist. get (I )). getlink (); author = (newsbean) newslist. get (I )). getauthor (); timestamp = (newsbean) newslist. get (I )). getpublishtime (); Id = string. valueof (newsbean) newslist. get (I )). getnewsid (); Description = (newsbean) newslist. get (I )). getsummary (); content = (newsbean) newslist. get (I )). getcontent (); rssitem = new rssitem (title, Link, author, timestamp, ID, description); rssitemlist. add (I, rssitem); atomitem = new atomitem (title, Link, author, timestamp, ID, description, content); atomitemlist. add (I, atomitem); I ++ ;}

Implementation skills

After successfully implementing the news publishing system, you may benefit from the following deployment skills.

Implement RSS or atom Summarization for the most popular digest reader

To meet the needs of the most popular abstract reader, pay attention to the following points when generating abstract files.

    • Add RSS and atom version information in the XML file header, as shown in listing 7:

      Listing 7. Sample Code for adding RSS and atom version information

      <? XML version = "1.0" encoding = "UTF-8"?> <RSS version = "2.0">... <? XML version = "1.0" encoding = "UTF-8"?> & Lt; atom version = "1.0" & gt;

    • Provides link information for RSS and atom channels. In RSS 2.0, the link information is displayed as a node, but in atom 1.0, the link information is displayed as an attribute. Although links are not mandatory for atom, it is a good practice to provide link information for the channel (see listing 8 ).

Listing 8. Sample Code that provides link information for the Channel

<? XML version = "1.0" encoding = "UTF-8" ???> <RSS?> <Channel?> <Title?> News syndication </Title?> <Link?> Http://www.newssyndication.com </link?> <Description?> This is a news feed </description?> ... <? XML version = "1.0" encoding = "UTF-8" ???> <Feed xmlns = "http://www.w3.org/2005/Atom"?> <Title?> News syndication </title> <link href = "http://www.newssyndication.com"/?> <Updated?> 2006-07-06t10: 26: 30z </updated?> <ID> urn: UUID: 60a76c80-d399-11d9-b93c-0003939e0af6 </ID?>

Support globalization when generating XML files

Add the encoding information to the XML file header.

Listing 9. Sample Code for adding encoding information to the XML File Header

<? XML version = "1.0" encoding = "UTF-8"?>

UseOutputstreamwriterReplaceFilewriterSet the encoding to UTF-8.FilewriterThe default encoding is "GBK ".

Listing 10. Sample Code that uses outputstreamwriter to set encoding to UTF-8

Outputstreamwriter writer = new outputstreamwriter (outputstream, "UTF-8 ");

Conclusion

In this article, we first review the similarities and differences between RSS and atom summaries, and then examine the architecture of the publishing system using the two abstract types. This architecture includes three subsystems that work together. Next, I will introduce some implementation skills to you after the step-by-step demonstration. Now you can use these efficient methods in news summaries.

References

Learning

    • For more information, see the original article on the developerworks global site.

    • What is Atom? : Atom quick start.
    • RSS 2.0 and atom 1.0, compared: details about the differences between RSS 2.0 and atom 1.0.
    • RSS 2.0 specification: Learn the entire specification.
    • RSS and atom feed: build your own RSS and atom digest after a deep dive into the developerworks site.
    • RSS Tutorial: Learn what RSS is, how to get started, and how to use it.
    • Workplace RSS feed and blog: Get the latest content from the workplace discussion forum, workplace product support page, developerworks workplace Article, and developerworks workplace tutorial.
    • Scheduled repeated tasks in Java applications (Tom White, developerworks, November 2003): Read aboutTimerClass.
    • Developerworks Web Development Area: articles and tutorials on WEB technology can improve your website development skills.
    • Developerworks technical events and network broadcasts: focus on and understand the latest developments in technology, shorten learning processes, and improve the quality and results of the most difficult software projects.

original address: http://www-128.ibm.com/developerworks/cn/web/wa-syndrssatom/index.html? CA = Drs-# listing1

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.