Simple JavaScript Object to XML string converter.
npm install node-json2xmlSimple JavaScript Object to XML string converter.
Install via npm, which will download json2xml and all of its dependencies.
``bash`
npm install json2xml
While the name of the repo is json2xml, it is really pojo2xml, since you will need to run JSON.parse on the JSON data prior to converting.
`js
var fs = require('fs');
var json2xml = require('json2xml');
fs.readFile('data.json', 'utf8', function read (err, data) {
if (err) console.log(err);
fs.writeFile('data.xml', json2xml(JSON.parse(data)));
});
`
`js
// none:
json2xml({ a: 1 });
//1
// empty node:
json2xml({ a: '' });
//
// add header:
json2xml({ a: 1 }, { header: true });
//1
// add node attributes:
json2xml({ a: 1, attr: { b: 2, c: 3 } }, { attributes_key: 'attr' });
// 1
// arrays:
json2xml([ { a: 1 }, { b: 2 } ]);
//'12
json2xml({ 'items': [ { item: 1 }, { item: 2 } ] });
//'
``