UNPKG

xml-to-json-promise

Version:

Convert an XML file or XML data to JSON (via xml2js), with promises.

98 lines (58 loc) 2.51 kB
# xml-to-json-promise [![Build Status](https://travis-ci.org/radiovisual/xml-to-json-promise.png?branch=master)](https://travis-ci.org/radiovisual/xml-to-json-promise) [![Coverage Status](https://coveralls.io/repos/github/radiovisual/xml-to-json-promise/badge.svg?branch=master)](https://coveralls.io/github/radiovisual/xml-to-json-promise?branch=master) > Convert an XML file or XML data to JSON (via xml2js), with promises. This module is a promise-supported wrapper around the fabulous [xml2js](https://github.com/Leonidas-from-XIV/node-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 ```js 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](https://github.com/Leonidas-from-XIV/node-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](https://github.com/Leonidas-from-XIV/node-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: ```js 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](https://github.com/Leonidas-from-XIV/node-xml2js) library, so please direct any issues/bugs regarding the parsing/handling of your JSON data directly to [xml2js](https://github.com/Leonidas-from-XIV/node-xml2js). Otherwise, feel free to open any issues if you discover a problem with this module. ## License MIT @ [Michael Wuergler](http://numetriclabs.com)