forked from matanshiloah/xml-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmlParser.ts
More file actions
174 lines (142 loc) · 4.31 KB
/
xmlParser.ts
File metadata and controls
174 lines (142 loc) · 4.31 KB
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
import { XMLTag } from "./xmlTag";
export class XMLParser {
private static _parseFromString(xmlText: string) {
xmlText = XMLParser._encodeCDATAValues(xmlText);
let cleanXmlText = xmlText
.replace(/\s{2,}/g, " ")
.replace(/\\t\\n\\r/g, "")
.replace(/>/g, ">\n")
.replace(/\]\]/g, "]]\n");
let rawXmlData: XMLTag[] = [];
cleanXmlText.split("\n").map((element) => {
element = element.trim();
if (!element || element.indexOf("?xml") > -1) {
return;
}
if (element.indexOf("<") == 0 && element.indexOf("CDATA") < 0) {
let parsedTag = XMLParser._parseTag(element);
if (parsedTag) {
rawXmlData.push(parsedTag);
if (element.match(/\/\s*>$/)) {
let closingTag = XMLParser._parseTag("</" + parsedTag.name + ">");
if (closingTag) {
rawXmlData.push(closingTag);
}
}
}
} else {
rawXmlData[rawXmlData.length - 1].value += ` ${XMLParser._parseValue(
element
)}`;
}
});
return XMLParser._convertTagsArrayToTree(rawXmlData)[0];
}
private static _encodeCDATAValues(xmlText: string) {
let cdataRegex = new RegExp(/<!\[CDATA\[([^\]\]]+)\]\]/gi);
let result = cdataRegex.exec(xmlText);
while (result) {
if (result.length > 1) {
xmlText = xmlText.replace(result[1], encodeURIComponent(result[1]));
}
result = cdataRegex.exec(xmlText);
}
return xmlText;
}
private static _parseTag(tagText: string): XMLTag | undefined {
let cleanTagText = tagText.match(
/([^\s]*)=('([^']*?)'|"([^"]*?)")|([\/?\w\-\:]+)/g
);
if (cleanTagText !== null && cleanTagText !== undefined) {
let tag: XMLTag = new XMLTag({
name: cleanTagText.shift()?.replace(/\/\s*$/, "") ?? "",
});
cleanTagText.map((attribute) => {
let attributeKeyVal = attribute.split("=");
if (attributeKeyVal.length < 2) {
return;
}
let attributeKey = attributeKeyVal[0];
let attributeVal = "";
if (attributeKeyVal.length === 2) {
attributeVal = attributeKeyVal[1];
} else {
attributeKeyVal = attributeKeyVal.slice(1);
attributeVal = attributeKeyVal.join("=");
}
tag.attributes[attributeKey] = attributeVal
.replace(/^"/g, "")
.replace(/^'/g, "")
.replace(/"$/g, "")
.replace(/'$/g, "")
.trim();
});
return tag;
}
return undefined;
}
private static _parseValue(tagValue: string) {
if (tagValue.indexOf("CDATA") < 0) {
return tagValue.trim();
}
return tagValue.substring(
tagValue.lastIndexOf("[") + 1,
tagValue.indexOf("]")
);
}
private static _convertTagsArrayToTree(xml: XMLTag[]) {
let xmlTree = [];
while (xml.length > 0) {
let tag = xml.shift();
if (
(tag?.value && tag.value.indexOf("</") > -1) ||
tag?.name.match(/\/$/)
) {
tag.name = tag.name.replace(/\/$/, "").trim();
tag.value = tag.value.substring(0, tag.value.indexOf("</")).trim();
xmlTree.push(tag);
continue;
}
if (tag?.name.indexOf("/") == 0) {
break;
}
if (tag) {
xmlTree.push(tag);
tag.children = XMLParser._convertTagsArrayToTree(xml);
tag.value = decodeURIComponent(tag.value.trim());
}
}
return xmlTree;
}
private static _toString(xml: XMLTag) {
var xmlText = XMLParser._convertTagToText(xml);
if (xml.children.length > 0) {
xml.children.map((child) => {
xmlText += XMLParser._toString(child);
});
xmlText += "</" + xml.name + ">";
}
return xmlText;
}
private static _convertTagToText(tag: XMLTag) {
var tagText = "<" + tag.name;
for (var attribute in tag.attributes) {
tagText += " " + attribute + '="' + tag.attributes[attribute] + '"';
}
if (tag.value.length > 0) {
tagText += ">" + tag.value;
} else {
tagText += ">";
}
if (tag.children.length === 0) {
tagText += "</" + tag.name + ">";
}
return tagText;
}
static parseFromString(xmlText: string) {
return XMLParser._parseFromString(xmlText);
}
static toString(xml: XMLTag) {
return XMLParser._toString(xml);
}
}