Node.js: How to create XML files

Is there a good way to create XML files? For example, like the Builder for Rails (or any other way)?

Thanks

Answers


It looks like the xmlbuilder-js library may do this for you. If you have npm installed, you can npm install xmlbuilder.

It will let you do this (taken from their example):

var builder = require('xmlbuilder');
var doc = builder.create('root');

doc.ele('xmlbuilder')
    .att('for', 'node-js')
    .ele('repo')
      .att('type', 'git')
      .txt('git://github.com/oozcitak/xmlbuilder-js.git') 
    .up()
  .up()
  .ele('test')
    .txt('complete');

console.log(doc.toString({ pretty: true }));

which will result in:

<root>
  <xmlbuilder for="node-js">
    <repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>
  </xmlbuilder>
  <test>complete</test>
</root>

recent changes to xmlbuilder require root element name passed to create()

see working example

var builder = require('xmlbuilder');
var doc = builder.create('root')
  .ele('xmlbuilder')
    .att('for', 'node-js')
    .ele('repo')
      .att('type', 'git')
      .txt('git://github.com/oozcitak/xmlbuilder-js.git') 
      .up()
    .up()
  .ele('test')
  .txt('complete')
.end({ pretty: true });
console.log(doc.toString());

Need Your Help

HTTPS error "data length too long" in s3_pkt.c from Socket.io

node.js ssl https socket.io ssl-certificate

We’re trying to get Socket.io flashsockets to work in Internet Explorer 9 over HTTPS/WSS. The flashsockets work over HTTP, but HTTPS is giving us problems. We’re using socket.io version 0.8.7 and s...

Can someone explain the "debounce" function in Javascript

javascript debouncing

I am interested in the "debouncing" function in javascript, written here : http://davidwalsh.name/javascript-debounce-function