Package Exports
- xml-to-json-promise
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 (xml-to-json-promise) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
xml-to-json-promise
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.
Install
npm install --save xml-to-json-promise
Usage
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);
});
API
convert.xmlFileToJSON(path, [options])
Converts an XML file to JSON. Returns a promise with the json data.
path
Required
Type: String
The path location to your xml file.
options
Type: object
The xml2js options you want to use when parsing the JSON.
convert.xmlDataToJSON(xml, [options])
Converts raw XML data to JSON. Returns a promise with the json data.
xml
Required
Type: String
The raw XML data you want to convert to JSON.
options
Type: object
The xml2js options you want to use when parsing the JSON.
Saving a JSON file
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!');
});
});
Notes
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.
License
MIT @ Michael Wuergler