diff --git a/lib/json2xml.js b/lib/json2xml.js index 3ce88ae..5521f3c 100644 --- a/lib/json2xml.js +++ b/lib/json2xml.js @@ -53,6 +53,8 @@ module.exports = function xml(json, opts) { var node = json[key], attributes = ''; + if(typeof node === 'undefined' || null === node) node = ''; + if(opts.attributes_key && json[opts.attributes_key]){ Object.keys(json[opts.attributes_key]).forEach(function(k){ attributes += util.format(' %s="%s"', k, json[opts.attributes_key][k]); diff --git a/test/json2xml_test.js b/test/json2xml_test.js index 69e5117..14feca9 100644 --- a/test/json2xml_test.js +++ b/test/json2xml_test.js @@ -32,8 +32,10 @@ exports['json2xml'] = { test.done(); }, 'opts': function(test) { - test.expect(6); + test.expect(8); test.equal(json2xml({a:''}),''); + test.equal(json2xml({a:null}),''); + test.equal(json2xml({a:undefined}),''); test.equal(json2xml({a:1}),'1'); test.equal(json2xml([{a:1}, {b:2}]),'12'); @@ -41,7 +43,7 @@ exports['json2xml'] = { test.equal(json2xml({ 'items':[{item:1},{item:2}]} ),'12'); test.equal(json2xml({a:1}, { header:true }),'1'); - test.equal(json2xml({a:1, attr:{b:2,c:3 }}, { attributes_key:'attr' }), '1'); + test.equal(json2xml({a:1, attr:{b:2,c:3 }}, { attributes_key:'attr' }), '1'); test.done(); }