forked from Kode/khamake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlWriter.js
50 lines (43 loc) · 1.08 KB
/
XmlWriter.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
"use strict";
const fs = require('fs-extra');
function printElement(elem, data, indents) {
for (let i = 0; i < indents; ++i) data += '\t';
if (typeof elem === 'string') {
data += '<!-- ' + elem + ' -->\n';
return data;
}
data += '<' + elem.n;
for (let a in elem) {
if (a === 'n') continue;
if (a === 'e') continue;
data += ' ' + a + '="' + elem[a] + '"';
}
if (elem.e === undefined || elem.e.length === 0) {
data += ' />\n';
}
else {
data += '>\n';
for (let e of elem.e) {
data = printElement(e, data, indents + 1);
}
for (let i = 0; i < indents; ++i) data += '\t';
data += '</' + elem.n + '>\n';
}
return data;
}
module.exports = function (xml, path) {
let data = '';
data += '<?xml version="1.0" encoding="utf-8"?>\n';
data += '<' + xml.n;
for (let a in xml) {
if (a === 'n') continue;
if (a === 'e') continue;
data += ' ' + a + '="' + xml[a] + '"';
}
data += '>\n';
for (let e of xml.e) {
data = printElement(e, data, 1);
}
data += '</' + xml.n + '>\n';
fs.outputFileSync(path, data);
};