Over a million developers have joined DZone.

Build An Atom XML Feed Using Node.js

DZone 's Guide to

Build An Atom XML Feed Using Node.js

· Web Dev Zone ·
Free Resource

Wesley Moore has a very useful project called: node-genx. It is a Nodejs binding to the Genx XML generation library. The project makes it very easy to produce an ATOM feed quickly using Nodejs. Of course the library is not limited to just producing ATOM XML, it can produce all sorts of XML (see this article). However, in today’s article I want to focus on producing an ATOM XML Feed.

The project has a very easy to read and understand example which you can find here. I’ll post the author’s example code below for this article’s sake:

var genx = require('genx');
var w = new genx.Writer();
w.on('data', function(data) {
// Declare the elements and attributes up-front
var ns      = w.declareNamespace('http://www.w3.org/2005/Atom', '');
var feed    = w.declareElement(ns, 'feed');
var title   = w.declareElement(ns, 'title');
var link    = w.declareElement(ns, 'link');
var updated = w.declareElement(ns, 'updated');
var author  = w.declareElement(ns, 'author');
var name    = w.declareElement(ns, 'name');
var id      = w.declareElement(ns, 'id');
var entry   = w.declareElement(ns, 'entry');
var summary = w.declareElement(ns, 'summary');
var href    = w.declareAttribute('href');
// This is not a processing instruction and as such can be generated by Genx
process.stdout.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
    .startElement(title).addText("Example Feed").endElement()
    .startElement(link).addAttribute(href, "http://example.org/").endElement()
      .startElement(name).addText("John Doe").endElement()
      .startElement(title).addText("Atom-Powered Robots Run Amok").endElement()
      .startElement(link).addAttribute(href, "http://example.org/2003/12/13/atom03").endElement()
      .startElement(summary).addText("Some text.").endElement()

This example creates a very minimal ATOM feed with one entry.

You can run the above code easily assuming you have Nodejs installed along with NPM (if you download the Nodejs package off of the main website then NPM is already bundled). I installed Nodejs v0.6.9 on OS X v10.7.2 without any issues. Open up the OS X terminal and type the following inside of a working folder:

npm install genx

That will perform a local installtion of the node-genx library. Go ahead an create a file called app.js and add the code from above into the empty file and save it.

To run it with a pretty output simple use the following command:

node app.js | xmllint --format -


<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Example Feed</title>
  <link href="http://example.org/"/>
    <name>John Doe</name>
    <title>Atom-Powered Robots Run Amok</title>
    <link href="http://example.org/2003/12/13/atom03"/>
    <summary>Some text.</summary>

You can find all the documentation here as well as a good follow-up article here.


From http://www.giantflyingsaucer.com/blog/?p=3591


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}