diff --git a/README.md b/README.md index 1360d58..09967d8 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,8 @@ console.log(json); // {"key1":[{"$t":"value1"}],"key2":{"key3":[{"$t":"value3"}]}} ``` +* `defaultRootTagName` - *string* - If there is one root element and the tag name matches this, we'll exclude the root from the generated JSON : *default : row* + ## dump(Object json, Object options) Transform a Javascript data structure (JSON) in XML string. **Return String.** @@ -105,6 +107,7 @@ Transform a Javascript data structure (JSON) in XML string. **Return String.** * `version` - *string* - Set version attribute of XML header (see header flag); *default : 1.0* * `encoding` - *string* - Set encoding attribute of XML header (see header flag); *default : UTF-8* * `specialChar` - *string* - Set the first character of XML tag ($t, $text, $cd, $cdata, $e, $element, $c, $comment); *default : $* +* `defaultRootTagName` - *string* - If there are multiple roots we'll wrap them with this element *default : row* ## tojson(String xml) @@ -124,4 +127,3 @@ Alias of dump. [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/touv/node-xml-mapping/trend.png)](https://bitdeli.com/free "Bitdeli Badge") - diff --git a/lib/xml-mapping.js b/lib/xml-mapping.js index f67e99c..df176e0 100755 --- a/lib/xml-mapping.js +++ b/lib/xml-mapping.js @@ -25,6 +25,7 @@ var nameShortTag = 'n'; exports.dump = function (obj, options) { options = options || {}; options.specialChar = options.specialChar || '$'; + options.defaultRootTagName = options.defaultRootTagName || default_tag_name; if (typeof obj != "object") { return obj; @@ -149,7 +150,7 @@ exports.dump = function (obj, options) { o = obj; } else { - o[default_tag_name] = obj; + o[options.defaultRootTagName] = obj; } parse(o); @@ -160,6 +161,7 @@ exports.load = function (str, options) { options = options || {}; options.specialChar = options.specialChar || '$'; options.longTag = options.longTag || false; + options.defaultRootTagName = options.defaultRootTagName || default_tag_name; if (typeof str != "string") { if (options.throwErrors) { @@ -279,7 +281,7 @@ exports.load = function (str, options) { }); } - if (stack.length == 1 && node.name == default_tag_name) { + if (stack.length == 1 && node.name == options.defaultRootTagName) { result = cattr(node.attributes); stack.push(result); } diff --git a/test/tojson.js b/test/tojson.js index fb41be9..62f75e5 100644 --- a/test/tojson.js +++ b/test/tojson.js @@ -27,6 +27,8 @@ exports['t02'] = function (test) { test.deepEqual(XMLMapping.load(input), { key1 : { keyA: 'value1', keyB: 'value2' }, key2 : { keyA: 'value1', keyB: 'value2' } }); input = ''; test.deepEqual(XMLMapping.load(input), { key1 : { keyA: 'value1', keyB: 'value2', keyC: 'value3' }, key2 : { keyA: 'value1', keyB: 'value2', keyC: 'value3' } }); + input = ''; + test.deepEqual(XMLMapping.load(input, {defaultRootTagName: 'root'}), { key1 : { keyA: 'value1', keyB: 'value2', keyC: 'value3' }, key2 : { keyA: 'value1', keyB: 'value2', keyC: 'value3' } }); test.done(); }; diff --git a/test/toxml.js b/test/toxml.js index ccd37fa..39ef77c 100644 --- a/test/toxml.js +++ b/test/toxml.js @@ -3,15 +3,15 @@ var input; exports['t00'] = function (test) { input = {}; - test.equal(XMLMapping.dump(input), '') + test.equal(XMLMapping.dump(input), '') input = 'string'; - test.equal(XMLMapping.dump(input), 'string') + test.equal(XMLMapping.dump(input), 'string') input = 1234; - test.equal(XMLMapping.dump(input), 1234) + test.equal(XMLMapping.dump(input), 1234) test.done(); }; exports['t01'] = function (test) { - input = { + input = { key : {} }; test.equal(XMLMapping.dump(input), ''); @@ -31,11 +31,11 @@ exports['t01'] = function (test) { test.done(); }; exports['t02'] = function (test) { - input = { + input = { key1 : {}, key2 : {} }; - test.equal(XMLMapping.dump(input), ''); + test.equal(XMLMapping.dump(input), ''); input = { key1 : { key: 'value' @@ -44,7 +44,7 @@ exports['t02'] = function (test) { key: 'value' } }; - test.equal(XMLMapping.dump(input), ''); + test.equal(XMLMapping.dump(input), ''); input = { key1 : { keyA: 'value1', @@ -56,20 +56,25 @@ exports['t02'] = function (test) { } }; test.equal(XMLMapping.dump(input), ''); + input = { + key1 : {}, + key2 : {} + }; + test.equal(XMLMapping.dump(input, {defaultRootTagName: 'root'}), ''); test.done(); }; exports['t03a'] = function (test) { input = { key : [] }; - test.equal(XMLMapping.dump(input), ''); + test.equal(XMLMapping.dump(input), ''); test.done(); } exports['t03b'] = function (test) { input = { key : [{},{}] }; - test.equal(XMLMapping.dump(input), ''); + test.equal(XMLMapping.dump(input), ''); test.done(); } exports['t03c'] = function (test) { @@ -113,7 +118,7 @@ exports['t05a'] = function (test) { input = { '#element' : [{ $cd : 'value'}, { '#cd' : 'value'}] }; - test.equal(XMLMapping.dump(input), ''); + test.equal(XMLMapping.dump(input), ''); test.done(); }; exports['t05b'] = function (test) { @@ -122,7 +127,7 @@ exports['t05b'] = function (test) { '#element' : [{ $t : 'amstra'}, { _t : 'mdram'}] } }; - test.equal(XMLMapping.dump(input), 'amstramdram'); + test.equal(XMLMapping.dump(input), 'amstramdram'); test.done(); }; exports['t06'] = function (test) { @@ -131,13 +136,13 @@ exports['t06'] = function (test) { '$t' : 1 } }; - test.equal(XMLMapping.dump(input), '1'); + test.equal(XMLMapping.dump(input), '1'); input = { key : { '$t' : 0 } }; - test.equal(XMLMapping.dump(input), '0'); + test.equal(XMLMapping.dump(input), '0'); test.done(); }; exports['t07'] = function (test) {