xml-to-json-promise

0.0.3 • Public • Published

xml-to-json-promise

Build Status Coverage Status

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

Package Sidebar

Install

npm i xml-to-json-promise

Weekly Downloads

1,780

Version

0.0.3

License

MIT

Last publish

Collaborators

  • radiovisual