forked from russplaysguitar/json2xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json2xml.js
40 lines (40 loc) · 1.35 KB
/
json2xml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* VERSION 0.2 (original author => [email protected])
* MODIFIED BY => [email protected]
*
* This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.
*/
var json2xml = (function (my, undefined) {
"use strict";
var tag = function(name, options) {
options = options || {};
return "<"+(options.closing ? "/" : "")+name+">";
};
var exports = {
convert:function(obj, rootname) {
var xml = "";
if(typeof obj == "object") {
for (var i in obj) {
if(obj.hasOwnProperty(i)){
var value = obj[i], type = typeof value;
if (value instanceof Array && type == 'object') {
xml += tag(i);
for (var j=0; j<=value.length-1; j++) {
xml += tag("item")+exports.convert(value[j])+tag("item",{closing:1});
}
xml += tag(i,{closing:1})
} else if (value instanceof Object && type == 'object') {
xml += tag(i)+exports.convert(value)+tag(i,{closing:1});
} else {
xml += tag(i)+value+tag(i,{closing:1});
}
}
}
} else {
xml += obj;
}
return rootname ? tag(rootname) + xml + tag(rootname,{closing:1}) : xml;
}
};
return exports;
})(json2xml || {});