DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. JavaScript
  4. Build An Atom XML Feed Using Node.js

Build An Atom XML Feed Using Node.js

Chad Lung user avatar by
Chad Lung
·
Mar. 09, 12 · Interview
Like (0)
Save
Tweet
Share
3.59K Views

Join the DZone community and get the full member experience.

Join For Free

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

XML Node.js Build (game engine)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Introduction to Container Orchestration
  • Master Spring Boot 3 With GraalVM Native Image
  • Best CI/CD Tools for DevOps: A Review of the Top 10
  • Front-End Troubleshooting Using OpenTelemetry

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: