xmlbuilder-js

An XML builder for node.js

README

xmlbuilder-js


An XML builder for node.js similar to
License NPM Version NPM Downloads
Travis Build Status AppVeyor Build status Dev Dependency Status Code Coverage

Announcing xmlbuilder2:


The new release of xmlbuilder is available at [xmlbuilder2](https://github.com/oozcitak/xmlbuilder2)! xmlbuilder2 has been redesigned from the ground up to be fully conforming to the modern DOM specification. It supports XML namespaces, provides built-in converters for multiple formats, collection functions, and more. Please see upgrading from xmlbuilder in the wiki.

New development will be focused towards xmlbuilder2; xmlbuilder will only receive critical bug fixes.

Installation:


  1. ``` sh
  2. npm install xmlbuilder
  3. ```

Usage:


  1. ``` js
  2. var builder = require('xmlbuilder');

  3. var xml = builder.create('root')
  4.   .ele('xmlbuilder')
  5.     .ele('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
  6.   .end({ pretty: true});

  7. console.log(xml);
  8. ```

will result in:

  1. ``` xml
  2. <?xml version="1.0"?>
  3. <root>
  4.   <xmlbuilder>
  5.     <repo type="git">git://github.com/oozcitak/xmlbuilder-js.git
  6.   </xmlbuilder>
  7. </root>
  8. ```

It is also possible to convert objects into nodes:

  1. ``` js
  2. var builder = require('xmlbuilder');

  3. var obj = {
  4.   root: {
  5.     xmlbuilder: {
  6.       repo: {
  7.         '@type': 'git', // attributes start with @
  8.         '#text': 'git://github.com/oozcitak/xmlbuilder-js.git' // text node
  9.       }
  10.     }
  11.   }
  12. };

  13. var xml = builder.create(obj).end({ pretty: true});
  14. console.log(xml);
  15. ```

If you need to do some processing:

  1. ``` js
  2. var builder = require('xmlbuilder');

  3. var root = builder.create('squares');
  4. root.com('f(x) = x^2');
  5. for(var i = 1; i <= 5; i++)
  6. {
  7.   var item = root.ele('data');
  8.   item.att('x', i);
  9.   item.att('y', i * i);
  10. }

  11. var xml = root.end({ pretty: true});
  12. console.log(xml);
  13. ```

This will result in:

  1. ``` xml
  2. <?xml version="1.0"?>
  3. <squares>
  4.   
  5.   <data x="1" y="1"/>
  6.   <data x="2" y="4"/>
  7.   <data x="3" y="9"/>
  8.   <data x="4" y="16"/>
  9.   <data x="5" y="25"/>
  10. </squares>
  11. ```

Documentation:

See the wiki for details and examples for more complex examples.

Donations:

Please consider becoming a backer or sponsor to help support development.

[Donate Button](https://opencollective.com/xmlbuilder)