forked from matanshiloah/xml-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlParser.js
129 lines (99 loc) · 3.66 KB
/
xmlParser.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
function XMLParser() {
function parseFromString(xmlText) {
xmlText = xmlText.replace(/\s{2,}/g, '').replace(/\\t\\n\\r/g, '').replace(/>/g, '>\n');
var tags = xmlText.split('\n');
var xml = [];
for (var i = 0; i < tags.length; i++) {
if (tags[i].indexOf('?xml') < 0) {
if (tags[i].indexOf('<') == 0 && tags[i].indexOf('CDATA') < 0) {
xml.push(parseTag(tags[i]));
} else {
xml[xml.length - 1].value = parseValue(tags[i]);
}
}
}
return convertTagsArrayToTree(xml)[0];
}
function parseTag(tagText, parent) {
tagText = tagText.replace('<', '').replace('>', '').split(' ');
var tag = {
name: tagText[0],
attributes: {},
children: [],
value: '',
getElementsByTagName: function (tagName) {
var matches = [];
if (tagName == '*' || this.name.toLowerCase() == tagName.toLowerCase()) {
matches.push(this);
}
for (var i = 0; i < this.children.length; i++) {
matches = matches.concat(this.children[i].getElementsByTagName(tagName));
}
return matches;
}
};
for (var i = 1; i < tagText.length; i++) {
var attribute = tagText[i].split('=');
tag.attributes[attribute[0]] = attribute[1].replace(/"/g, '').replace(/'/g, '').trim();
}
return tag;
}
function parseValue(tagValue) {
if (tagValue.indexOf('CDATA') < 0) {
return tagValue.trim();
}
return tagValue.substring(tagValue.lastIndexOf('[') + 1, tagValue.indexOf(']'));
}
function convertTagsArrayToTree(xml) {
var xmlTree = [];
if (xml.length == 0) {
return xmlTree;
}
var tag = xml.shift();
if (tag.value.indexOf('</') > -1) {
tag.value = tag.value.substring(0, tag.value.indexOf('</'));
xmlTree.push(tag);
xmlTree = xmlTree.concat(convertTagsArrayToTree(xml));
return xmlTree;
}
if (tag.name.indexOf('/') == 0) {
return xmlTree;
}
xmlTree.push(tag);
tag.children = convertTagsArrayToTree(xml);
xmlTree = xmlTree.concat(convertTagsArrayToTree(xml));
return xmlTree;
}
function toString(xml) {
var xmlText = convertTagToText(xml);
if (xml.children.length > 0) {
for (var i = 0; i < xml.children.length; i++) {
xmlText += toString(xml.children[i]);
}
xmlText += '</' + xml.name + '>';
}
return xmlText;
}
function convertTagToText(tag) {
var tagText = '<' + tag.name;
var attributesText = [];
for (var attribute in tag.attributes) {
tagText += ' ' + attribute + '="' + tag.attributes[attribute] + '"';
}
if (tag.value.length > 0) {
tagText += '>' + tag.value + '</' + tag.name + '>';
} else {
tagText += '>';
}
return tagText;
}
return {
parseFromString: function (xmlText) {
return parseFromString(xmlText);
},
toString: function (xml) {
return toString(xml);
}
};
}
module.exports = XMLParser;