-
Notifications
You must be signed in to change notification settings - Fork 53
/
x2js.js
768 lines (632 loc) · 23.8 KB
/
x2js.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*
Copyright 2015 Axinom
Copyright 2011-2013 Abdulla Abdurakhmanov
Original sources are available at https://code.google.com/p/x2js/
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Supported export methods:
* AMD
* <script> (window.X2JS)
* Node.js
Limitations:
* Attribute namespace prefixes are not parsed as such.
* Overall the serialization/deserializaton code is "best effort" and not foolproof.
*/
// Module definition pattern used is returnExports from https://github.com/umdjs/umd
(function (root, factory) {
"use strict";
/* global define */
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but only CommonJS-like
// environments that support module.exports, like Node.
module.exports = factory(require("@xmldom/xmldom").DOMParser);
} else {
// Browser globals (root is window)
root.X2JS = factory();
}
})(this, function (CustomDOMParser) {
"use strict";
// We return a constructor that can be used to make X2JS instances.
return function X2JS(config) {
var VERSION = "3.4.4";
config = config || {};
function initConfigDefaults() {
// If set to "property" then <element>_asArray will be created
// to allow you to access any element as an array (even if there is only one of it).
config.arrayAccessForm = config.arrayAccessForm || "none";
// If "text" then <empty></empty> will be transformed to "".
// If "object" then <empty></empty> will be transformed to {}.
config.emptyNodeForm = config.emptyNodeForm || "text";
// Function that will be called for each elements, if the function returns true, the element will be skipped
// function(name, value) { return true; };
config.jsAttributeFilter = config.jsAttributeFilter;
// Function that will be called for each elements, the element value will be replaced by the returned value
// function(name, value) { return parseFloat(value); };
config.jsAttributeConverter = config.jsAttributeConverter;
// Allows attribute values to be converted on the fly during parsing to objects.
// "test": function(name, value) { return true; }
// "convert": function(name, value) { return parseFloat(value); };
// convert() will be called for every attribute where test() returns true
// and the return value from convert() will replace the original value of the attribute.
config.attributeConverters = config.attributeConverters || [];
// Any elements that match the paths here will have their text parsed
// as an XML datetime value (2011-11-12T13:00:00-07:00 style).
// The path can be a plain string (parent.child1.child2),
// a regex (/.*\.child2/) or function(elementPath).
config.datetimeAccessFormPaths = config.datetimeAccessFormPaths || [];
// Any elements that match the paths listed here will be stored in JavaScript objects
// as arrays even if there is only one of them. The path can be a plain string
// (parent.child1.child2), a regex (/.*\.child2/) or function(elementName, elementPath).
config.arrayAccessFormPaths = config.arrayAccessFormPaths || [];
// xmldom constructor arguments
// @see https://github.com/jindw/xmldom#api-reference
config.xmldomOptions = config.xmldomOptions || {};
// If true, a toString function is generated to print nodes containing text or cdata.
// Useful if you want to accept both plain text and CData as equivalent inputs.
if (config.enableToStringFunc === undefined) {
config.enableToStringFunc = true;
}
// If true, empty text tags are ignored for elements with child nodes.
if (config.skipEmptyTextNodesForObj === undefined) {
config.skipEmptyTextNodesForObj = true;
}
// If true, whitespace is trimmed from text nodes.
if (config.stripWhitespaces === undefined) {
config.stripWhitespaces = true;
}
// If true, double quotes are used in generated XML.
if (config.useDoubleQuotes === undefined) {
config.useDoubleQuotes = true;
}
// If true, the root element of the XML document is ignored when converting to objects.
// The result will directly have the root element's children as its own properties.
if (config.ignoreRoot === undefined) {
config.ignoreRoot = false;
}
// Whether XML characters in text are escaped when reading/writing XML.
if (config.escapeMode === undefined) {
config.escapeMode = true;
}
// Prefix to use for properties that are created to represent XML attributes.
if (config.attributePrefix === undefined) {
config.attributePrefix = "_";
}
// If true, empty elements will created as self closing elements (<element />)
// If false, empty elements will be created with start and end tags (<element></element>)
if (config.selfClosingElements === undefined) {
config.selfClosingElements = true;
}
// If this property defined as false and an XML element has CData node ONLY, it will be converted to text without additional property "__cdata"
if (config.keepCData === undefined) {
config.keepCData = false;
}
// If this property defined as true, use { __text: 'abc' } over 'abc'
if (config.keepText === undefined) {
config.keepText = false;
}
// If true, will output dates in UTC
if (config.jsDateUTC === undefined) {
config.jsDateUTC = false;
}
}
function initRequiredPolyfills() {
function pad(number) {
var r = String(number);
if (r.length === 1) {
r = '0' + r;
}
return r;
}
// Hello IE8-
if (typeof String.prototype.trim !== 'function') {
String.prototype.trim = function trim() {
return this.replace(/^\s+|^\n+|(\s|\n)+$/g, '');
};
}
if (typeof Date.prototype.toISOString !== 'function') {
// Implementation from http://stackoverflow.com/questions/2573521/how-do-i-output-an-iso-8601-formatted-string-in-javascript
Date.prototype.toISOString = function toISOString() {
var MS_IN_S = 1000;
return this.getUTCFullYear()
+ '-' + pad(this.getUTCMonth() + 1)
+ '-' + pad(this.getUTCDate())
+ 'T' + pad(this.getUTCHours())
+ ':' + pad(this.getUTCMinutes())
+ ':' + pad(this.getUTCSeconds())
+ '.' + String((this.getUTCMilliseconds() / MS_IN_S).toFixed(3)).slice(2, 5)
+ 'Z';
};
}
}
initConfigDefaults();
initRequiredPolyfills();
var DOMNodeTypes = {
"ELEMENT_NODE": 1,
"TEXT_NODE": 3,
"CDATA_SECTION_NODE": 4,
"COMMENT_NODE": 8,
"DOCUMENT_NODE": 9
};
function getDomNodeLocalName(domNode) {
var localName = domNode.localName;
if (localName == null) {
// Yeah, this is IE!!
localName = domNode.baseName;
}
if (localName == null || localName === "") {
// ==="" is IE too
localName = domNode.nodeName;
}
return localName;
}
function getDomNodeNamespacePrefix(node) {
return node.prefix;
}
function escapeXmlChars(str) {
if (typeof str === "string")
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
else
return str;
}
function unescapeXmlChars(str) {
return str.replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, "'").replace(/&/g, '&');
}
function ensureProperArrayAccessForm(element, childName, elementPath) {
switch (config.arrayAccessForm) {
case "property":
if (!(element[childName] instanceof Array))
element[childName + "_asArray"] = [element[childName]];
else
element[childName + "_asArray"] = element[childName];
break;
}
if (!(element[childName] instanceof Array) && config.arrayAccessFormPaths.length > 0) {
var match = false;
for (var i = 0; i < config.arrayAccessFormPaths.length; i++) {
var arrayPath = config.arrayAccessFormPaths[i];
if (typeof arrayPath === "string") {
if (arrayPath === elementPath) {
match = true;
break;
}
} else if (arrayPath instanceof RegExp) {
if (arrayPath.test(elementPath)) {
match = true;
break;
}
} else if (typeof arrayPath === "function") {
if (arrayPath(childName, elementPath)) {
match = true;
break;
}
}
}
if (match)
element[childName] = [element[childName]];
}
}
function xmlDateTimeToDate(prop) {
// Implementation based up on http://stackoverflow.com/questions/8178598/xml-datetime-to-javascript-date-object
// Improved to support full spec and optional parts
var MINUTES_PER_HOUR = 60;
var bits = prop.split(/[-T:+Z]/g);
var d = new Date(bits[0], bits[1] - 1, bits[2]);
var secondBits = bits[5].split("\.");
d.setHours(bits[3], bits[4], secondBits[0]);
if (secondBits.length > 1)
d.setMilliseconds(secondBits[1]);
// Get supplied time zone offset in minutes
if (bits[6] && bits[7]) {
var offsetMinutes = bits[6] * MINUTES_PER_HOUR + Number(bits[7]);
var sign = /\d\d-\d\d:\d\d$/.test(prop) ? '-' : '+';
// Apply the sign
offsetMinutes = 0 + (sign === '-' ? -1 * offsetMinutes : offsetMinutes);
// Apply offset and local timezone
d.setMinutes(d.getMinutes() - offsetMinutes - d.getTimezoneOffset());
} else if (prop.indexOf("Z", prop.length - 1) !== -1) {
d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()));
}
// d is now a local time equivalent to the supplied time
return d;
}
function convertToDateIfRequired(value, childName, fullPath) {
if (config.datetimeAccessFormPaths.length > 0) {
var pathWithoutTextNode = fullPath.split("\.#")[0];
for (var i = 0; i < config.datetimeAccessFormPaths.length; i++) {
var candidatePath = config.datetimeAccessFormPaths[i];
if (typeof candidatePath === "string") {
if (candidatePath === pathWithoutTextNode)
return xmlDateTimeToDate(value);
} else if (candidatePath instanceof RegExp) {
if (candidatePath.test(pathWithoutTextNode))
return xmlDateTimeToDate(value);
} else if (typeof candidatePath === "function") {
if (candidatePath(pathWithoutTextNode))
return xmlDateTimeToDate(value);
}
}
}
return value;
}
function deserializeRootElementChildren(rootElement) {
var result = {};
var children = rootElement.childNodes;
// Alternative for firstElementChild which is not supported in some environments
for (var i = 0; i < children.length; i++) {
var child = children.item(i);
if (child.nodeType === DOMNodeTypes.ELEMENT_NODE) {
var childName = getDomNodeLocalName(child);
if (config.ignoreRoot)
result = deserializeDomChildren(child, childName);
else
result[childName] = deserializeDomChildren(child, childName);
}
}
return result;
}
function deserializeElementChildren(element, elementPath) {
var result = {};
result.__cnt = 0;
var nodeChildren = element.childNodes;
// Child nodes.
for (var iChild = 0; iChild < nodeChildren.length; iChild++) {
var child = nodeChildren.item(iChild);
var childName = getDomNodeLocalName(child);
if (child.nodeType === DOMNodeTypes.COMMENT_NODE)
continue;
result.__cnt++;
// We deliberately do not accept everything falsey here because
// elements that resolve to empty string should still be preserved.
if (result[childName] == null) {
result[childName] = deserializeDomChildren(child, elementPath + "." + childName);
ensureProperArrayAccessForm(result, childName, elementPath + "." + childName);
} else {
if (!(result[childName] instanceof Array)) {
result[childName] = [result[childName]];
ensureProperArrayAccessForm(result, childName, elementPath + "." + childName);
}
result[childName][result[childName].length] = deserializeDomChildren(child, elementPath + "." + childName);
}
}
// Attributes
for (var iAttribute = 0; iAttribute < element.attributes.length; iAttribute++) {
var attribute = element.attributes.item(iAttribute);
result.__cnt++;
var adjustedValue = attribute.value;
for (var iConverter = 0; iConverter < config.attributeConverters.length; iConverter++) {
var converter = config.attributeConverters[iConverter];
if (converter.test.call(null, attribute.name, attribute.value))
adjustedValue = converter.convert.call(null, attribute.name, attribute.value);
}
result[config.attributePrefix + attribute.name] = adjustedValue;
}
// Node namespace prefix
var namespacePrefix = getDomNodeNamespacePrefix(element);
if (namespacePrefix) {
result.__cnt++;
result.__prefix = namespacePrefix;
}
if (result["#text"]) {
result.__text = result["#text"];
if (result.__text instanceof Array) {
result.__text = result.__text.join("\n");
}
if (config.escapeMode)
result.__text = unescapeXmlChars(result.__text);
if (config.stripWhitespaces)
result.__text = result.__text.trim();
delete result["#text"];
if (config.arrayAccessForm === "property")
delete result["#text_asArray"];
result.__text = convertToDateIfRequired(result.__text, "#text", elementPath + ".#text");
}
if (result.hasOwnProperty('#cdata-section')) {
result.__cdata = result["#cdata-section"];
delete result["#cdata-section"];
if (config.arrayAccessForm === "property")
delete result["#cdata-section_asArray"];
}
if (result.__cnt === 1 && result.__text && !config.keepText) {
result = result.__text;
} else if (result.__cnt === 0 && config.emptyNodeForm === "text") {
result = '';
} else if (result.__cnt > 1 && result.__text !== undefined && config.skipEmptyTextNodesForObj) {
if (config.stripWhitespaces && result.__text === "" || result.__text.trim() === "") {
delete result.__text;
}
}
delete result.__cnt;
/**
* We are checking if we are creating a __cdata property or if we just add the content of cdata inside result.
* But, if we have a property inside xml tag (<tag PROPERTY="1"></tag>), and a cdata inside, we can't ignore it.
* In this case we are keeping __cdata property.
*/
if (!config.keepCData && (!result.hasOwnProperty('__text') && result.hasOwnProperty('__cdata') && Object.keys(result).length === 1)) {
return (result.__cdata ? result.__cdata : '');
}
if (config.enableToStringFunc && (result.__text || result.__cdata)) {
result.toString = function toString() {
return (this.__text ? this.__text : '') + (this.__cdata ? this.__cdata : '');
};
}
return result;
}
function deserializeDomChildren(node, parentPath) {
if (node.nodeType === DOMNodeTypes.DOCUMENT_NODE) {
return deserializeRootElementChildren(node);
} else if (node.nodeType === DOMNodeTypes.ELEMENT_NODE) {
return deserializeElementChildren(node, parentPath);
} else if (node.nodeType === DOMNodeTypes.TEXT_NODE || node.nodeType === DOMNodeTypes.CDATA_SECTION_NODE) {
return node.nodeValue;
} else {
return null;
}
}
function serializeStartTag(jsObject, elementName, attributeNames, selfClosing) {
var resultStr = "<" + ((jsObject && jsObject.__prefix) ? (jsObject.__prefix + ":") : "") + elementName;
if (attributeNames) {
for (var i = 0; i < attributeNames.length; i++) {
var attributeName = attributeNames[i];
var attributeValue = jsObject[attributeName];
if (config.escapeMode)
attributeValue = escapeXmlChars(attributeValue);
resultStr += " " + attributeName.substr(config.attributePrefix.length) + "=";
if (config.useDoubleQuotes)
resultStr += '"' + attributeValue + '"';
else
resultStr += "'" + attributeValue + "'";
}
}
if (!selfClosing)
resultStr += ">";
else
resultStr += " />";
return resultStr;
}
function serializeEndTag(jsObject, elementName) {
return "</" + ((jsObject && jsObject.__prefix) ? (jsObject.__prefix + ":") : "") + elementName + ">";
}
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
function isSpecialProperty(jsonObj, propertyName) {
if ((config.arrayAccessForm === "property" && endsWith(propertyName.toString(), ("_asArray")))
|| propertyName.toString().indexOf(config.attributePrefix) === 0
|| propertyName.toString().indexOf("__") === 0
|| (jsonObj[propertyName] instanceof Function))
return true;
else
return false;
}
function getDataElementCount(jsObject) {
var count = 0;
if (jsObject instanceof Object) {
for (var propertyName in jsObject) {
if (isSpecialProperty(jsObject, propertyName))
continue;
count++;
}
}
return count;
}
function getDataAttributeNames(jsObject) {
var names = [];
if (jsObject instanceof Object) {
for (var attributeName in jsObject) {
if (attributeName.toString().indexOf("__") === -1
&& attributeName.toString().indexOf(config.attributePrefix) === 0) {
names.push(attributeName);
}
}
}
return names;
}
function serializeComplexTextNodeContents(textNode) {
var result = "";
if (textNode.__cdata) {
result += "<![CDATA[" + textNode.__cdata + "]]>";
}
if (textNode.__text || typeof (textNode.__text) === 'number' || typeof (textNode.__text) === 'boolean') {
if (config.escapeMode)
result += escapeXmlChars(textNode.__text);
else
result += textNode.__text;
}
return result;
}
function serializeTextNodeContents(textNode) {
var result = "";
if (textNode instanceof Object) {
result += serializeComplexTextNodeContents(textNode);
} else if (textNode !== null) {
if (config.escapeMode)
result += escapeXmlChars(textNode);
else
result += textNode;
}
return result;
}
function serializeArray(elementArray, elementName, attributes) {
var result = "";
if (elementArray.length === 0) {
result += serializeStartTag(elementArray, elementName, attributes, true);
} else {
for (var i = 0; i < elementArray.length; i++) {
result += serializeJavaScriptObject(elementArray[i], elementName, getDataAttributeNames(elementArray[i]));
}
}
return result;
}
function serializeJavaScriptObject(element, elementName, attributes) {
var result = "";
// Filter out elements
if (config.jsAttributeFilter && config.jsAttributeFilter.call(null, elementName, element)) {
return result;
}
// Convert element
if (config.jsAttributeConverter) {
element = config.jsAttributeConverter.call(null, elementName, element);
}
if ((element === undefined || element === null || element === '') && config.selfClosingElements) {
result += serializeStartTag(element, elementName, attributes, true);
} else if (typeof element === 'object') {
if (Object.prototype.toString.call(element) === '[object Array]') {
result += serializeArray(element, elementName, attributes);
} else if (element instanceof Date) {
result += serializeStartTag(element, elementName, attributes, false);
// Serialize date
result += config.jsDateUTC ? element.toUTCString() : element.toISOString();
result += serializeEndTag(element, elementName);
} else {
var childElementCount = getDataElementCount(element);
if (childElementCount > 0 || typeof (element.__text) === 'number' || typeof (element.__text) === 'boolean' || element.__text || element.__cdata) {
result += serializeStartTag(element, elementName, attributes, false);
result += serializeJavaScriptObjectChildren(element);
result += serializeEndTag(element, elementName);
} else if (config.selfClosingElements) {
result += serializeStartTag(element, elementName, attributes, true);
} else {
result += serializeStartTag(element, elementName, attributes, false);
result += serializeEndTag(element, elementName);
}
}
} else {
result += serializeStartTag(element, elementName, attributes, false);
result += serializeTextNodeContents(element);
result += serializeEndTag(element, elementName);
}
return result;
}
function serializeJavaScriptObjectChildren(jsObject) {
var result = "";
var elementCount = getDataElementCount(jsObject);
if (elementCount > 0) {
for (var elementName in jsObject) {
if (isSpecialProperty(jsObject, elementName))
continue;
var element = jsObject[elementName];
var attributes = getDataAttributeNames(element);
result += serializeJavaScriptObject(element, elementName, attributes);
}
}
result += serializeTextNodeContents(jsObject);
return result;
}
function parseXml(xml) {
if (xml === undefined) {
return null;
}
if (typeof xml !== "string") {
return null;
}
var parser = null;
var domNode = null;
if (CustomDOMParser) {
// This branch is used for node.js, with the xmldom parser.
parser = new CustomDOMParser(config.xmldomOptions);
domNode = parser.parseFromString(xml, "text/xml");
} else if (window && window.DOMParser) {
parser = new window.DOMParser();
var parsererrorNS = null;
var isIEParser = window.ActiveXObject || "ActiveXObject" in window;
// IE9+ now is here
if (!isIEParser && document.all && !document.addEventListener) {
try {
parsererrorNS = parser.parseFromString("INVALID", "text/xml").childNodes[0].namespaceURI;
} catch (err) {
parsererrorNS = null;
}
}
try {
domNode = parser.parseFromString(xml, "text/xml");
if (parsererrorNS !== null && domNode.getElementsByTagNameNS(parsererrorNS, "parsererror").length > 0) {
domNode = null;
}
} catch (err) {
domNode = null;
}
} else {
// IE :(
if (xml.indexOf("<?") === 0) {
xml = xml.substr(xml.indexOf("?>") + 2);
}
/* global ActiveXObject */
domNode = new ActiveXObject("Microsoft.XMLDOM");
domNode.async = "false";
domNode.loadXML(xml);
}
return domNode;
}
this.asArray = function asArray(prop) {
if (prop === undefined || prop === null) {
return [];
} else if (prop instanceof Array) {
return prop;
} else {
return [prop];
}
};
this.toXmlDateTime = function toXmlDateTime(dt) {
if (dt instanceof Date) {
return dt.toISOString();
} else if (typeof (dt) === 'number') {
return new Date(dt).toISOString();
} else {
return null;
}
};
this.asDateTime = function asDateTime(prop) {
if (typeof (prop) === "string") {
return xmlDateTimeToDate(prop);
} else {
return prop;
}
};
/*
Internally the logic works in a cycle:
DOM->JS - implemented by custom logic (deserialization).
JS->XML - implemented by custom logic (serialization).
XML->DOM - implemented by browser.
*/
// Transformns an XML string into DOM-tree
this.xml2dom = function xml2dom(xml) {
return parseXml(xml);
};
// Transforms a DOM tree to JavaScript objects.
this.dom2js = function dom2js(domNode) {
return deserializeDomChildren(domNode, null);
};
// Transforms JavaScript objects to a DOM tree.
this.js2dom = function js2dom(jsObject) {
var xml = this.js2xml(jsObject);
return parseXml(xml);
};
// Transformns an XML string into JavaScript objects.
this.xml2js = function xml2js(xml) {
var domNode = parseXml(xml);
if (domNode != null)
return this.dom2js(domNode);
else
return null;
};
// Transforms JavaScript objects into an XML string.
this.js2xml = function js2xml(jsObject) {
return serializeJavaScriptObjectChildren(jsObject);
};
this.getVersion = function getVersion() {
return VERSION;
};
};
});