xmlbuilder2 logo

An XML builder for node.js.

Installation:

npm install xmlbuilder2

Usage:

xmlbuilder2 is a wrapper around a DOM implementation which adds chainable functions so that complex XML documents can be created easily. Since an XML document is a tree of nodes, function chaining naturally follows the structure of the document resulting in more readable source code. For example, the following XML document:

<?xml version='1.0'?>
<root att='val'>
  <foo>
    <bar>foobar</bar>
  </foo>
  <baz/>
</root>

can be created with:

const { create } = require('xmlbuilder2');

const doc = create({ version: '1.0' })
  .ele('root', { att: 'val' })
    .ele('foo')
      .ele('bar').txt('foobar').up()
    .up()
    .ele('baz')
    .doc();

console.log(doc.end({ prettyPrint: true }));

create function is exported by the module, and it creates and returns a blank XML document node, ele function creates and returns an element node and up function returns its parent element node. You can think of up as the closing tag of its element node. doc function returns the document node of the XML document. Finally, the end function converts the XML document into its string representation. end can convert into other formats as explained here.


A nested JS object can also be thought of as a tree (although with the restriction that its keys should be unique) so xmlbuilder2 also supports converting to/from JS objects into XML nodes. The same example with JS objects becomes:

const { create } = require('xmlbuilder2');

const doc = create({ version: '1.0' }, {
  root: {
    '@att': 'val',
    foo: {
      bar: "foobar"
    },
    baz: {}
  }
});

console.log(doc.end({ prettyPrint: true }));

xmlbuilder2 can parse and serialize XML documents in different formats. For example:

const { create } = require('xmlbuilder2');

const xmlStr = '<root att="val"><foo/><bar>foobar</bar></foo></root>';

const doc = create(xmlStr);
// append a 'baz' element to the root node of the document
doc.root().ele('baz');

console.log(doc.end({ prettyPrint: true }));

which would result in the XML document at the top of this page. See parsing and serialization pages.


xmlbuilder2 can convert between different formats. For example:

const { convert } = require('xmlbuilder2');

const xmlStr = '<root att="val"><foo/><bar>foobar</bar></foo></root>';

const obj = convert(xmlStr, { format: 'object' });
console.log(obj);
{
  root: {
    '@att': 'val',
    'foo': {
      'bar': 'foobar'
    }
  }
}

See the convert function.

Donations:

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

Donate Button