JSPM

  • Created
  • Published
  • Downloads 1594401
  • Score
    100M100P100Q189682F
  • License MIT

An XML builder for node.js

Package Exports

  • xmlbuilder2

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (xmlbuilder2) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

xmlbuilder2

An XML builder for node.js.

License NPM Version

Travis Build Status AppVeyor Build status Dev Dependency Status Code Coverage

Installation:

npm install xmlbuilder2

Usage:

import { document } from 'xmlbuilder2';

const root = document()
  .ele('topgun')
    .ele('pilots')
      .ele('pilot', { 'callsign': 'Iceman', 'rank': 'Lieutenant' }).txt('Tom Kazansky').up()
      .ele('pilot', { 'callsign': 'Maverick', 'rank': 'Lieutenant' }).txt('Pete Mitchell').up()
      .ele('pilot', { 'callsign': 'Goose', 'rank': 'Lieutenant (j.g.)' }).txt('Nick Bradshaw').up()
    .up()
    .ele('hangar')
      .ele('aircraft').txt('F-14 Tomcat').up()
      .ele('aircraft').txt('MiG-28').up()
    .up()
  .up()

// convert the XML tree to string
const xml = root.end({ prettyPrint: true });
console.log(xml);

will result in:

<?xml version="1.0"?>
<topgun>
  <pilots>
    <pilot callsign="Iceman" rank="Lieutenant">Tom Kazansky</pilot>
    <pilot callsign="Maverick" rank="Lieutenant">Pete Mitchell</pilot>
    <pilot callsign="Goose" rank="Lieutenant (j.g.)">Nick Bradshaw</pilot>
  </pilots>
  <hangar>
    <aircraft>F-14 Tomcat</aircraft>
    <aircraft>MiG-28</aircraft>
  </hangar>
</topgun>

The same XML document can be created by converting a JS object into XML nodes:

import { document } from 'xmlbuilder2';

const obj = {
  topgun: {
    pilots: {
      pilot: [
        { '@callsign': 'Iceman', '@rank': 'Lieutenant', '#': 'Tom Kazansky' },
        { '@callsign': 'Maverick', '@rank': 'Lieutenant', '#': 'Pete Mitchell' },
        { '@callsign': 'Goose', '@rank': 'Lieutenant (j.g.)', '#': 'Nick Bradshaw' }
      ]
    },
    hangar: {
      aircraft: ['F-14 Tomcat', 'MiG-28']
    }
  }
}

const doc = document(obj);
const xml = doc.end({ prettyPrint: true });
console.log(xml);

xmlbuilder2 can also parse and serialize XML documents from different formats:

import { document } from 'xmlbuilder2';

const xmlStr = '<root att="val"><foo/><bar>foobar</bar></foo></root>';
const doc = document(xmlStr);

// append a "baz" element to the root node of the document
doc.root().ele("baz");

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

which would output:

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

or you could return a JS object by changing the format argument to "object":

const obj = doc.end({ format: "object" });
{
  root: {
    "@att": "val",
    "foo": {
      "bar": "foobar"
    }
    "baz": {}
  }
}

If you need to do some processing:

import { document } from 'xmlbuilder2';

const root = document().ele('squares');
root.com('f(x) = x^2');
for(let i = 1; i <= 5; i++)
{
  const item = root.ele('data');
  item.att('x', i;
  item.att('y', i * i);
}

const xml = root.end({ prettyPrint: true });
console.log(xml);

This will result in:

<?xml version="1.0"?>
<squares>
  <!-- f(x) = x^2 -->
  <data x="1" y="1"/>
  <data x="2" y="4"/>
  <data x="3" y="9"/>
  <data x="4" y="16"/>
  <data x="5" y="25"/>
</squares>

See the wiki for details.