forked from sTiLL-iLL/x2js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
samples.html
362 lines (306 loc) · 11.6 KB
/
samples.html
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="xml2json.min.js"></script>
<!-- script type="text/javascript" src="xml2json.js"></script-->
</head>
<body>
<h1>X2JS samples html</h1>
<script type="text/javascript">
// Create x2js instance with default config
var x2js = new X2JS();
// Available options
var x2jsOptionsSample = new X2JS({
// Escaping XML characters. Default is true from v1.1.0+
escapeMode : true,
// XML attributes prefix. Default is "_"
attributePrefix : "_",
// Array access form (none|property).
// Use this property if you want X2JS generate additional property <element>_asArray
// to access in array form any element
// Default is 'none' from v1.1.0+
arrayAccessForm : "none",
// Handling empty nodes (text|object).
// When X2JS found empty node like <test></test> it will be transformed to test : '' for 'text' mode,
// or to Object for 'object' mode
// Default is 'text'
emptyNodeForm : "text",
// Enable/Disable auxiliary function in generated JSON object to print text nodes with __text/__cdata
// Default is true
enableToStringFunc : true,
// Array access paths (array).
// Use this option to configure paths to XML elements always in "array form".
// You can configure beforehand paths to all your array elements based on XSD or your knowledge
// about XML structure
// Every path could be a simple string (like 'parent.child1.child2'), a regex (like /.*\.child2/), or a custom function
// Default is empty array
arrayAccessFormPaths : [],
// Strip whitespaces (trimming text nodes)
stripWhitespaces : true,
// Skip empty text tags for nodes with children
skipEmptyTextNodesForObj : true,
// DateTime access paths (array).
// Use this option to configure paths to XML elements for "datetimes form".
// You can configure beforehand paths to all your array elements based on XSD or your knowledge
// about XML structure
// Every path could be a simple string (like 'parent.child1.child2'), a regex (like /.*\.child2/), or a custom function
// Default is empty array
datetimeAccessFormPaths : []
});
// JSON to DOM
var xmlDoc = x2js.json2xml(
{
MyRoot : {
MyChild : 'my_child_value',
MyAnotherChild: 10,
MyArray : [ 'test', 'test2' ],
MyArrayRecords : [
{
ttt : 'vvvv'
},
{
ttt : 'vvvv2'
}
]
}
}
);
// JSON to XML string
var xmlDocStr = x2js.json2xml_str(
{
MyRoot : {
MyChild : 'my_child_value',
MyAnotherChild : 10,
MyArray : [ 'test', 'test2' ],
MyArrayRecords : [
{
ttt : 'vvvv'
},
{
ttt : 'vvvv2'
}
]
}
}
);
console.log(xmlDocStr);
// JSON arrays to string
var xmlDocStr = x2js.json2xml_str(
{
MyRoot : {
namedItemArray: {
item : [
{ first: 'success1' } ,
{ first: 'success2' }
]
},
namedArray: [
{ first: 'success1' } ,
{ first: 'success2' }
],
justArray: [ 'just success1', 'just success2' ],
arrayWithAttrs : [
{
_test : 'successAttr',
__text : 'success',
temp : 'successTemp'
},
{
_test : 'successAttr2',
__text : 'success2',
temp : 'successTemp2'
}
]
}
}
);
console.log(xmlDocStr);
// XML string to JSON
var xmlText = "<MyOperation><test>Success</test><test2><item>ddsfg</item><item>dsdgfdgfd</item></test2></MyOperation>";
var jsonObj = x2js.xml_str2json( xmlText );
console.log(jsonObj.MyOperation.test);
// Array access form examples
console.log(x2js.asArray(jsonObj.MyOperation.test)[0]);
// Or old style (1.0.+):
x2jsOld = new X2JS({arrayAccessForm : "property"});
jsonObj = x2jsOld.xml_str2json( xmlText );
console.log("Old is "+jsonObj.MyOperation.test_asArray[0]);
// XML/DOM to JSON
var xmlText = " <MyOperation> <test>- Success -</test> <test2> TestText <item> ddsfg </item> TestText2 <item>dsdgfdgfd</item></test2></MyOperation>"
xmlDoc=x2js.parseXmlString(xmlText);
var jsonObj = x2js.xml2json( xmlDoc );
console.log(jsonObj.MyOperation.test);
// Parsing XML attrs
var xmlText = "<MyOperation myAttr='SuccessAttrValue'><txtAttrChild sAttr='SUCCESS TXT ATTR CHILD'>SUCCESS TXT</txtAttrChild><test>Success</test><test2 myAttr='SuccessAttrValueTest2'><item>ddsfg</item><item>dsdgfdgfd</item></test2></MyOperation>";
var jsonObj = x2js.xml_str2json( xmlText );
console.log(jsonObj.MyOperation._myAttr);
console.log(jsonObj.MyOperation.test2._myAttr);
console.log(jsonObj.MyOperation.txtAttrChild._sAttr);
console.log(jsonObj.MyOperation.txtAttrChild.__text);
console.log(jsonObj.MyOperation.txtAttrChild.toString());
// JSON to XML attrs
var xmlDocStr = x2js.json2xml_str(
{
TestAttrRoot : {
_myAttr : 'myAttrValue',
MyChild : 'my_child_value',
MyAnotherChild: 10,
MyTextAttrChild : {
_myTextAttr : 'myTextAttrValue',
__text : 'HelloText'
}
}
}
);
console.log(xmlDocStr);
//Change prefix for attributes
var x2jsChangedAttrs = new X2JS({
// XML attributes. Default is "_"
attributePrefix : "$$"
});
jsonObj = x2jsChangedAttrs.xml_str2json( xmlText );
console.log(jsonObj.MyOperation.$$myAttr);
console.log(jsonObj.MyOperation.test2.$$myAttr);
console.log(jsonObj.MyOperation.txtAttrChild.$$sAttr);
xmlDocStr = x2jsChangedAttrs.json2xml_str({
TestAttrRoot : {
_myAttr : 'myAttrValue',
MyChild : 'my_child_value',
MyAnotherChild: 10,
MyTextAttrChild : {
$$myTextAttr : 'myTextAttrValue',
__text : 'HelloText'
}
}
}
);
console.log(xmlDocStr);
// Parse XML with namespaces
var xmlText = "<testns:MyOperation xmlns:testns='http://www.example.org'><test>Success</test><test2 myAttr='SuccessAttrValueTest2'><item>ddsfg</item><item>dsdgfdgfd</item><item2>testArrSize</item2></test2></testns:MyOperation>";
var jsonObj = x2js.xml_str2json( xmlText );
console.log(jsonObj.MyOperation.test);
if(jsonObj.MyOperation.test2.item.length > 2)
console.log("Error! Incorrect array len!");
var testObjC = {
'm:TestAttrRoot' : {
'_tns:m' : 'http://www.example.org',
'_tns:cms' : 'http://www.example.org',
MyChild : 'my_child_value',
'cms:MyAnotherChild' : 'vdfd'
}
}
// Parse JSON object with namespaces
var xmlDocStr = x2js.json2xml_str(
testObjC
);
console.log(xmlDocStr);
// Parse JSON object constructed with another NS-style
var testObjNew = {
TestAttrRoot : {
__prefix : 'm',
'_tns:m' : 'http://www.example.org',
'_tns:cms' : 'http://www.example.org',
MyChild : 'my_child_value',
MyAnotherChild : {
__prefix : 'cms',
__text : 'vdfd'
}
}
}
// Parse JSON object with namespaces
var xmlDocStr = x2js.json2xml_str(
testObjNew
);
console.log(xmlDocStr);
// Parse XML with header
var xmlText = "<?xml version='1.0' encoding='utf-8' ?>\n"+
"<test>XML HEADER SUCCESS!</test>";
var jsonObj = x2js.xml_str2json( xmlText );
console.log(jsonObj.test);
// Parse XML with CDATA
var xmlText = "<!-- Root comment --><test><simple>simple success</simple><data><![CDATA[<CDATA success/>]]></data> </test>";
var jsonObj = x2js.xml_str2json( xmlText );
console.log(jsonObj.test.data.toString());
console.log(jsonObj.test.data.__cdata);
console.log(jsonObj.test.simple);
// Parse JSON object with CDATA
var xmlDocStr = x2js.json2xml_str(
jsonObj
);
console.log(xmlDocStr);
// Parse JSON with emtpy attributes
var xmlDocStr = x2js.json2xml_str(
{
MyRoot : {
MyNullChild : null,
MyNullChild2 : undefined,
MyAnotherChild : 10,
MyEmptyChild : {
_attr : "test"
},
MyEmptyChild2 : {
_attr : "test",
__text : "Empty Nodes Test"
}
}
}
);
console.log(xmlDocStr);
// Escaping XML characters
xmlDocStr = x2js.json2xml_str(
{
MyRoot : {
MyEscapeXmlChild : "<success> & \" ' / </success>",
MyEscapeXmlChild2 : {
_attr : "success&",
__text : "<success> & \" ' / </success>"
},
MyEscapeXmlChildNonString : false
}
}
);
console.log(xmlDocStr);
jsonObj = x2js.xml_str2json( xmlDocStr );
console.log(jsonObj.MyRoot.MyEscapeXmlChild);
console.log(jsonObj.MyRoot.MyEscapeXmlChild2._attr);
console.log(jsonObj.MyRoot.MyEscapeXmlChild2.toString());
// Array access path
x2js = new X2JS({
arrayAccessFormPaths : [
"MyArrays.test4.item",
/.*\.test3\.item/
]
});
xmlText = "<MyArrays>"+
"<test2><item>success</item><item>second</item></test2>"+
"<test3><item>success</item></test3>"+
"<test4><item>success</item></test4>"+
"<test5><item>success</item></test5>"+
"</MyArrays>";
jsonObj = x2js.xml_str2json( xmlText );
console.log(jsonObj.MyArrays.test3.item[0]);
console.log(jsonObj.MyArrays.test4.item[0]);
console.log(jsonObj.MyArrays.test5.item);
// Working with datetimes
x2js = new X2JS({
datetimeAccessFormPaths : [
"MyDts.testds",
/.*\.testdt.*/
]
});
xmlText = "<MyDts>"+
"<testds>2002-10-10T12:00:00+04:00</testds>"+
"<testdt1>2002-10-10T12:00:00Z</testdt1>"+
"<testdt2>2002-10-10T12:00:00</testdt2>"+
"<testdc>2002-10-10T12:00:00Z</testdc>"+
"</MyDts>";
jsonObj = x2js.xml_str2json( xmlText );
console.log(jsonObj.MyDts.testds);
console.log(jsonObj.MyDts.testdt1);
console.log(jsonObj.MyDts.testdt2);
console.log(x2js.asDateTime( jsonObj.MyDts.testdc ));
console.log(x2js.getVersion());
console.log('-- ALL TESTS COMPLETED! --');
</script>
</body>
</html>