json-xml-parser
v1.0.5
Published
A module for converting between XML format and JSON objects
Downloads
6
Readme
JSON-XML-Parser
A module for converting between XML format and JSON objects
installation
npm install json-xml-parser
usage
In order to convert JSON object to XML:
var parser = require('json-xml-parser');
var json = {
number: 1,
string: 'string',
obj: {
field: 'value'
},
array_name: ['element1', 'element2']
};
var xml = parser.jsonToXml(json);
Now xml is the root element of an xml tree that looks like this:
<data>
<number>1</number>
<string>string</string>
<obj>
<field>value</field>
</obj>
<array_nameArray>
<array_name>element1</array_name>
<array_name>element2</array_name>
</array_nameArray>
</data>
It's possible to provide a dictionary that defines the names of the XML elements. E.g.:
var parser = require('json-xml-parser');
var json = {
number: 1,
string: 'string',
obj: {
field: 'value'
},
array_name: ['element1', 'element2']
};
var xml = parser.jsonToXml(json, { head: 'root', array_name: 'arr' });
In this case the XML will look like this:
<root>
<number>1</number>
<string>string</string>
<obj>
<field>value</field>
</obj>
<arrArray>
<arr>element1</arr>
<arr>element2</arr>
</arrArray>
</root>