Parses xml and converts xml to and from JavaScript objects.
This library is distributed under the MIT license, its source and full documentation can be found at www.npmjs.com/package/xml2js
Executed
Server-side.
Details
var xml2js = require('xml2js');
This module converts xml to a JavaScript object. It can also convert that object to a JSON string, build xml from an object and has a wide variety of other options. See the documentation linked to above for more information.
Example
This example converts xml to a JavaScript object.
function(params, credentials) {
var r;
var xml2js = require('xml2js');
var parser = new xml2js.Parser();
var xml = "<people><person><name>Tim</name><age>34</age></person><person><name>Leo</name><age>33</age></person></people>";
parser.parseString(xml, function (err, result) {
r = result;
});
return r;
}
{
"jsonrpc": "2.0",
"id": 174,
"result": {
"people": {
"person": [{
"name": ["Tim"],
"age": ["34"]
}, {
"name": ["Leo"],
"age": ["33"]
}]
}
}
}
Arrays
In the example above you can see the that child nodes have been placed in arrays, this is the default behaviour. Set
function(params, credentials) {
var r;
var xml2js = require('xml2js');
var parser = new xml2js.Parser({
explicitArray: false
});
var xml = "<people><person><name>Tim</name><age>34</age></person><person><name>Leo</name><age>33</age></person></people>";
parser.parseString(xml, function(err, result) {
r = result;
});
return r;
}
Returns
{
"jsonrpc": "2.0",
"id": 15,
"result": {
"people": {
"person": [{
"name": "Tim",
"age": "34"
}, {
"name": "Leo",
"age": "33"
}]
}
}
}