forked from soldair/node-jsontoxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsontoxml.js
163 lines (140 loc) · 3.55 KB
/
jsontoxml.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
copyright Ryan Day 2010 <http://ryanday.org> [MIT Licensed]
THIS MODULE EXPORTS 2 METHODS:
exports.json_to_xml
exports.obj_to_xml
ARGUMENTS:
obj
the object you would like to render to xml
in the case of json_to_xml this is a string which is "JSON.parse"d for you
add_xml_header
boolean if you would like the xml header included in the output
NOTE: you can always add it you your output by passing it in your input if the default doesnt work for you
'<?xml version="1.0" encoding="utf-8"?>'
EXAMPLE USE:
jsonxml.obj_to_xml({
node:'text content',
parent:[
{name:'taco',text:'beef taco',children:{salsa:'hot!'}},
{name:'taco',text:'fish taco',attrs:{mood:'sad'},children:[
{name:'salsa',text:'mild'},
'hi',
{name:'salsa',text:'weak',attrs:{type:2}}
]},
{name:'taco',attrs:'mood="party!"'}
],
parent2:{
hi:'is a nice thing to say',
node:'i am another not special child node',
date:function(){
return (new Date())+'';
}
}
})
outputs: // ! output is not tabbed this is an example
<node>text content</node>
<parent>
<taco>
beef taco
<salsa>hot!</salsa>
</taco>
<taco>
fish taco
<salsa>mild</salsa>
hi
<salsa type="2">weak</salsa>
</taco>
<taco mood='party!'/>
</parent>
<parent2>
<hi>is a nice thing to say</hi>
<node>i am another not special child node</node>
<date>Sun Sep 26 2010 17:27:29 GMT-0700 (PDT)</date>
</parent2>
<node>text content</node>
<parent>
<taco>beef taco</taco>
<taco mood="sad">
fish taco
<salsa>mild</salsa>
hi
<salsa type="2">weak</salsa>
</taco>
<taco mood="party!"/>
</parent>
<parent2>
<hi>is a nice thing to say</hi>
<node>i am another not special child node</node>
<date>Mon Sep 27 2010 19:04:44 GMT-0700 (PDT)</date>
</parent2>
*/
var process_to_xml = function fn(node_data,node_descriptor){
var xml = "";
//if value is an array create child nodes from values
if(node_data instanceof Array){
node_data.map(function(v){
xml += fn(v,1);
//entries that are values of an array are the only ones that can be special node descriptors
});
} else {
switch(typeof node_data){
case 'object':
if(node_descriptor == 1 && node_data.name){
var
content = "",
name = node_data.name,
attributes = "";
if(node_data.attrs) {
if(typeof node_data.attrs != 'object') {
attributes +=' '+node_data.attrs;
node_data.attrs = {};
}
var attrs = node_data.attrs;
for(var i in attrs){
attributes += ' '+i+'="'+attrs[i]+'"';
}
}
//later attributes can be added here
if(node_data.text || node_data.value) {
content += (node_data.value || '')+(node_data.text || '');
}
if(node_data.children){
content += fn(node_data.children);
}
if(content.length) {
xml +='<'+name+attributes+'>'+content+'</'+name+'>';
} else {
xml +='<'+name+attributes+'/>';
}
} else {
for( var i in node_data){
var content = fn(node_data[i]);
if(content.length) {
xml +='<'+i+'>'+content+'</'+i+'>';
} else {
xml +='<'+i+'/>';
}
}
}
break;
case 'function':
xml += node_data(xml,fn);
break;
default:
xml += node_data+'';
}
}
return xml;
};
var xml_header = '<?xml version="1.0" encoding="utf-8"?>';
exports.json_to_xml = function(json,add_xml_header){
try{
var obj = JSON.parse(json);
} catch(e){
return false;
}
return this.obj_to_xml(obj,add_xml_header);
}
exports.obj_to_xml = function(obj,add_xml_header){
return (add_xml_header?xml_header:'')+process_to_xml(obj);
}