-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXML.js
109 lines (104 loc) · 2.55 KB
/
XML.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
import { DOMParser } from "https://code4fukui.github.io/xmldom-es/xmldom.js";
const parseNode = (xmlNode, result) => {
if (xmlNode.nodeName == null) {
return;
} else if (xmlNode.nodeName == "#text") {
const v = xmlNode.nodeValue.trim();
if (v) {
result["#text"] = v;
}
return;
} else if (xmlNode.childNodes == undefined) {
result["#text"] = xmlNode.data;
return;
}
const json = {};
const existing = result[xmlNode.nodeName];
if (existing) {
if (!Array.isArray(existing)) {
result[xmlNode.nodeName] = [existing, json];
} else {
result[xmlNode.nodeName].push(json);
}
} else {
result[xmlNode.nodeName] = json;
}
if (xmlNode.attributes) {
const len = xmlNode.attributes.length;
for (let i = 0; i < len; i++) {
const a = xmlNode.attributes[i];
json[a.nodeName] = a.nodeValue;
}
}
const len = xmlNode.childNodes.length;
for (let i = 0; i < len; i++) {
parseNode(xmlNode.childNodes[i], json);
}
};
const parseXML = (xml) => {
const result = {};
const dom = new DOMParser().parseFromString(xml, "text/xml");
const len = dom.childNodes.length;
for (let i = 0; i < len; i++) {
parseNode(dom.childNodes[i], result);
}
return result;
};
const stringifyXML = (json) => {
const res = [];
const stringifyNode = (tag, json, indent) => {
if (Array.isArray(json)) {
for (const o of json) {
stringifyNode(tag, o, indent);
}
return;
}
// attributes
const atts = [];
let bodyflg = false;
for (const name in json) {
const o = json[name];
if (name != "#text" && typeof o != "object") {
atts.push(name + '="' + o + '"');
} else {
bodyflg = true;
}
}
if (!bodyflg) {
if (atts.length == 0) {
res.push(`${indent}<${tag}/>`);
} else {
res.push(`${indent}<${tag} ${atts.join(" ")}/>`);
}
return;
}
if (atts.length == 0) {
res.push(`${indent}<${tag}>`);
} else {
res.push(`${indent}<${tag} ${atts.join(" ")}>`);
}
for (const name in json) {
const o = json[name];
if (typeof o == "object") {
stringifyNode(name, o, indent + " ");
}
}
const txt = json["#text"];
if (txt) {
res.push(`${indent} ${txt}`);
}
res.push(`${indent}</${tag}>`);
};
for (const tag in json) {
stringifyNode(tag, json[tag], "");
}
return res.join("\n");
};
export class XML {
static toJSON(sxml) {
return parseXML(sxml);
}
static stringify(json) {
return stringifyXML(json);
}
};