-
Notifications
You must be signed in to change notification settings - Fork 0
/
json2xml.js
48 lines (48 loc) · 1.43 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
41
42
43
44
45
46
47
48
/**
* VERSION // 0.1
* AUTHOR // [email protected]
*
* LICENSE //
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2004 Sam Hocevar <[email protected]>
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*/
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 = "";
for (var i in obj) {
if(obj.hasOwnProperty(i)){
var value = obj[i], type = typeof value;
if (value instanceof Array && type == 'object') {
for (var sub in value) {
xml += exports.convert(value[sub]);
}
} 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});
}
}
}
return rootname ? tag(rootname) + xml + tag(rootname,{closing:1}) : xml;
}
};
return exports;
})(json2xml || {});