"Programming is an art form that fights back."

Writing An Atom Feed

Sunday 23rd October 2005

Categories: Guides, Internet, Code

Writing Each Entry

Next, we need to create the entries that make up the bulk of the feed. We start the entry with:

<entry>

Each entry must have a unique ID. The easiest way to do this for each entry is to take the web address; remove everything before the domain name; insert a comma after the domain name, followed by the year, day and month in the format YYYY-DD-MM that the article was published, then a colon. Finally, add tag: at the beginning. The result might be:

<id>tag:example.com,2005-10-22:beans/newrecord.html</id>

Be sure to convert any hashes (#) into forward slashes (/).

Next, we need a title and the last time the entry was updated, e.g.

<title>New Bean Eating Record</title>
<updated>2005-10-22T21:20:45Z</updated>

To essentially copy what I've written before: Note that the title should be escaped, e.g. an ampersand should be written &amp;. By adding the appropriate attribute, you can also use (X)HTML markup for the title, but I feel that it is better to keep the feed simple, and not overburden it with details such as formatting. If you really must use HTML formatting, the easiest way is to use XHTML, otherwise each tag must be escaped. For example:

<title type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
New <b>Bean Eating</b> Record
</div>
</title>

The date/time should be in the format: YYYY-MM-DDTHH-MM-DDtimezoneoffset, replacing timezoneoffset with either the timezone offset from GMT, or a Z if there is no offset. Possible examples are:

<updated>2005-10-22T21:20:45Z</updated>
<updated>2005-10-22T21:20:45+01:00</updated>

There are a number of other elements that you can enter. There must be an author element for every single entry unless the feed already has an author. You can add as many author entries as you like. The format is:

<author>
<name>Bob Jones</name>
<email>bobjones@example.com</email>
<uri>http://example.com/~bobjones</uri>
</author>

You should include the name - the email address and URI are optional. As before, you can also add some contributors in the entry, e.g.

<contributor>
<name>Betty Jones</name>
<email>bettyjones@example.com</email>
<uri>http://example.com/~bettyjones</uri>
</contributor>

<contributor>
<name>Brian Jones</name>
</contributor>

A summary or a content element should also be present - if not one, then the other. Again, these can both be written as text or XHTML, so I won't repeat what I've already said - I'm sure you can glance up the page to the title element! The content element should contain the entire story, whereas the summary is, as the name suggests, a short summary. You could have:

<summary>A man has broken the bean eating record by eating over 20,000 baked beans in under five minutes.</summary>

An alternative might be:

<content>A man has broken the bean eating record by eating over 20,000 baked beans in under five minutes. Hasn't he done well! Here is an interview from the man himself: blah blah blah etc. etc. etc.</content>

The last element that you should really include is another link element. This should point to the web page that contains the full story, especially if there is no content tag. The format is the same as before:

<link rel="alternate" href="http://www.example.com/beans/newrecord.html"/>

Finally finish off the entry with </entry>.

That should leave you with something along the lines of:

<entry>
<id>tag:example.com,2005-10-22:beans/newrecord.html</id>
<title>New Bean Eating Record</title>
<summary>A man has broken the bean eating record by eating over 20,000 baked beans in under five minutes.</summary>
<updated>2005-10-22T21:20:45Z</updated>
<link rel="alternate" href="http://www.example.com/beans/newrecord.html"/>

<author>
<name>Bob Jones</name>
<email>bobjones@example.com</email>
<uri>http://example.com/~bobjones</uri>
</author>
</entry>

Each entry can be added in the same way.