Convert an XML file or XML data to JSON (via xml2js), with promises.
This module is a promise-supported wrapper around the fabulous xml2js library. This module makes it easy to convert XML files, as well as raw XML data to the JSON format.
npm install --save xml-to-json-promise
var convert = require('xml-to-json-promise');
// convert an xml file to json
convert.xmlFileToJSON('xmlfile.xml').then(json => {
console.log(json);
});
// convert raw xml data to json
convert.xmlDataToJSON('<example>data</example>').then(json => {
console.log(json);
});
Converts an XML file to JSON. Returns a promise with the json data.
Required
Type: String
The path location to your xml file.
Type: object
The xml2js options you want to use when parsing the JSON.
Converts raw XML data to JSON. Returns a promise with the json data.
Required
Type: String
The raw XML data you want to convert to JSON.
Type: object
The xml2js options you want to use when parsing the JSON.
Here is a recipe for saving your JSON to a file using xml-to-json-promise:
var convert = require('xml-to-json-promise');
var fs = require('fs');
convert.xmlDataToJSON('<example>data</example>').then(json => {
fs.writeFile('file.json', JSON.stringify(json), err => {
if (err) { throw err };
console.log('file saved!');
});
});
This is a wrapper around the xml2js library, so please direct any issues/bugs regarding the parsing/handling of your JSON data directly to xml2js. Otherwise, feel free to open any issues if you discover a problem with this module.
MIT @ Michael Wuergler