-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml4js.js
97 lines (83 loc) · 1.85 KB
/
xml4js.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
/*
* xml4js v1.0.0 by poliveira89 (github)
* Copyright 2013 Paulo Oliveira
* Licensed under MIT License
* <URL>
*/
function xml4js(options = {}) {
this.depth = 0;
this.encoding = options.encoding || 'UTF-8';
this.tabs = options.tabs == undefined ? false : options.tabs;
var declaration = options.declaration == undefined ? true : options.declaration;
this.xml = declaration ? '<?xml version="1.0" encoding="' + this.encoding + '"?>\n' : '';
}
xml4js.prototype.write = function() {
var context = this,
elem = null,
attr = null,
value = null,
callback = false,
fn = null,
hasValue = null,
empty = null;
elem = arguments[0];
if (typeof arguments[1] == 'object') {
attr = arguments[1];
}
else if (typeof arguments[1] == 'function') {
callback = true;
fn = arguments[1];
}
else {
value = arguments[1];
}
if (typeof arguments[2] == 'function') {
if (!callback) {
callback = true;
fn = arguments[2];
}
}
else {
if (!value)
value = arguments[2];
}
hasValue = value ? true : false;
hasChilds = callback ? true : false;
empty = !hasValue && !hasChilds;
function indent(depth) {
if (depth == null)
return '';
var space = '';
for (var i=0; i<depth; i++)
if (context.tabs)
space += "\t";
else
space += ' ';
return space;
}
if (elem == 'cdata') {
this.xml += indent(this.depth) + '<![CDATA[' + value + ']]>\n';
}
else {
this.xml += indent(this.depth) + '<' + elem;
if (attr) {
this.xml += ' ';
for (var key in attr)
this.xml += key + '="' + attr[key] + '"';
}
if (value)
this.xml += '>' + value + '</' + elem + '>\n';
if (callback) {
this.xml += '>\n'
this.depth++;
fn();
this.depth--;
this.xml += indent(this.depth) + '</' + elem + '>\n'
}
if (empty)
this.xml += ' />\n';
}
}
xml4js.prototype.toString = function() {
return this.xml;
}