Over a million developers have joined DZone.

Build An Atom XML Feed Using Node.js

· Web Dev Zone

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) {
  process.stdout.write(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");
w.startDocument()
  .startElement(feed)
    .startElement(title).addText("Example Feed").endElement()
    .startElement(link).addAttribute(href, "http://example.org/").endElement()
    .startElement(updated).addText("2003-12-13T18:30:02Z").endElement()
    .startElement(author)
      .startElement(name).addText("John Doe").endElement()
    .endElement()
    .startElement(id).addText("urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6").endElement()
    .startElement(entry)
      .startElement(title).addText("Atom-Powered Robots Run Amok").endElement()
      .startElement(link).addAttribute(href, "http://example.org/2003/12/13/atom03").endElement()
      .startElement(id).addText("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a").endElement()
      .startElement(updated).addText("2003-12-13T18:30:02Z").endElement()
      .startElement(summary).addText("Some text.").endElement()
    .endElement()
  .endElement()
.endDocument();

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 -

Output:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Example Feed</title>
  <link href="http://example.org/"/>
  <updated>2003-12-13T18:30:02Z</updated>
  <author>
    <name>John Doe</name>
  </author>
  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
  <entry>
    <title>Atom-Powered Robots Run Amok</title>
    <link href="http://example.org/2003/12/13/atom03"/>
    <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
    <updated>2003-12-13T18:30:02Z</updated>
    <summary>Some text.</summary>
  </entry>
</feed>

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

 

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

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}