-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-Dreambox-xml2json.js
181 lines (142 loc) · 7.01 KB
/
MMM-Dreambox-xml2json.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
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
175
176
177
178
179
180
181
'use strict';
module.exports = {
xml2json: xml2json
};
//***********************************************************************
// Main function. Clears the given xml and then starts the recursion
//***********************************************************************
function xml2json(xmlStr){
xmlStr = cleanXML(xmlStr);
return xml2jsonRecurse(xmlStr,0);
}
//***********************************************************************
// Recursive function that creates a JSON object with a given XML string.
//***********************************************************************
function xml2jsonRecurse(xmlStr) {
var obj = {},
tagName, indexClosingTag, inner_substring, tempVal, openingTag;
while (xmlStr.match(/<[^\/][^>]*>/)) {
openingTag = xmlStr.match(/<[^\/][^>]*>/)[0];
tagName = openingTag.substring(1, openingTag.length - 1);
indexClosingTag = xmlStr.indexOf(openingTag.replace('<', '</'));
// account for case where additional information in the openning tag
if (indexClosingTag == -1) {
tagName = openingTag.match(/[^<][\w+$]*/)[0];
indexClosingTag = xmlStr.indexOf('</' + tagName);
if (indexClosingTag == -1) {
indexClosingTag = xmlStr.indexOf('<\\/' + tagName);
}
}
inner_substring = xmlStr.substring(openingTag.length, indexClosingTag);
if (inner_substring.match(/<[^\/][^>]*>/)) {
tempVal = xml2json(inner_substring);
}
else {
tempVal = inner_substring;
}
// account for array or obj //
if (obj[tagName] === undefined) {
obj[tagName] = tempVal;
}
else if (Array.isArray(obj[tagName])) {
obj[tagName].push(tempVal);
}
else {
obj[tagName] = [obj[tagName], tempVal];
}
xmlStr = xmlStr.substring(openingTag.length * 2 + 1 + inner_substring.length);
}
return obj;
}
//*****************************************************************
// Removes some characters that would break the recursive function.
//*****************************************************************
function cleanXML(xmlStr) {
xmlStr = xmlStr.replace( /<!--[\s\S]*?-->/g, '' ); //remove commented lines
xmlStr = xmlStr.replace(/\n|\t|\r/g, ''); //replace special characters
xmlStr = xmlStr.replace(/ {1,}<|\t{1,}</g, '<'); //replace leading spaces and tabs
xmlStr = xmlStr.replace(/> {1,}|>\t{1,}/g, '>'); //replace trailing spaces and tabs
xmlStr = xmlStr.replace(/<\?[^>]*\?>/g, ''); //delete docType tags
xmlStr = replaceSelfClosingTags(xmlStr); //replace self closing tags
xmlStr = replaceAloneValues(xmlStr); //replace the alone tags values
xmlStr = replaceAttributes(xmlStr); //replace attributes
return xmlStr;
}
//************************************************************************************************************
// Replaces all the self closing tags with attributes with another tag containing its attribute as a property.
// The function works if the tag contains multiple attributes.
//
// Example : '<tagName attrName="attrValue" />' becomes
// '<tagName><attrName>attrValue</attrName></tagName>'
//************************************************************************************************************
function replaceSelfClosingTags(xmlStr) {
var selfClosingTags = xmlStr.match(/<[^/][^>]*\/>/g);
if (selfClosingTags) {
for (var i = 0; i < selfClosingTags.length; i++) {
var oldTag = selfClosingTags[i];
var tempTag = oldTag.substring(0, oldTag.length - 2);
tempTag += ">";
var tagName = oldTag.match(/[^<][\w+$]*/)[0];
var closingTag = "</" + tagName + ">";
var newTag = "<" + tagName + ">";
var attrs = tempTag.match(/(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/g);
if (attrs) {
for(var j = 0; j < attrs.length; j++) {
var attr = attrs[j];
var attrName = attr.substring(0, attr.indexOf('='));
var attrValue = attr.substring(attr.indexOf('"') + 1, attr.lastIndexOf('"'));
newTag += "<" + attrName + ">" + attrValue + "</" + attrName + ">";
}
}
newTag += closingTag;
xmlStr = xmlStr.replace(oldTag, newTag);
}
}
return xmlStr;
}
//*************************************************************************************************
// Replaces all the tags with attributes and a value with a new tag.
//
// Example : '<tagName attrName="attrValue">tagValue</tagName>' becomes
// '<tagName><attrName>attrValue</attrName><_@attribute>tagValue</_@attribute></tagName>'
//*************************************************************************************************
function replaceAloneValues(xmlStr) {
var tagsWithAttributesAndValue = xmlStr.match(/<[^\/][^>][^<]+\s+.[^<]+[=][^<]+>{1}([^<]+)/g);
if (tagsWithAttributesAndValue) {
for(var i = 0; i < tagsWithAttributesAndValue.length; i++) {
var oldTag = tagsWithAttributesAndValue[i];
var oldTagName = oldTag.substring(0, oldTag.indexOf(">") + 1);
var oldTagValue = oldTag.substring(oldTag.indexOf(">") + 1);
var newTag = oldTagName + "<_@ttribute>" + oldTagValue + "</_@ttribute>";
xmlStr = xmlStr.replace(oldTag, newTag);
}
}
return xmlStr;
}
//*****************************************************************************************************************
// Replaces all the tags with attributes with another tag containing its attribute as a property.
// The function works if the tag contains multiple attributes.
//
// Example : '<tagName attrName="attrValue"></tagName>' becomes '<tagName><attrName>attrValue</attrName></tagName>'
//*****************************************************************************************************************
function replaceAttributes(xmlStr) {
var tagsWithAttributes = xmlStr.match(/<[^\/][^>][^<]+\s+.[^<]+[=][^<]+>/g);
if (tagsWithAttributes) {
for (var i = 0; i < tagsWithAttributes.length; i++) {
var oldTag = tagsWithAttributes[i];
var tagName = oldTag.match(/[^<][\w+$]*/)[0];
var newTag = "<" + tagName + ">";
var attrs = oldTag.match(/(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/g);
if (attrs) {
for(var j = 0; j < attrs.length; j++) {
var attr = attrs[j];
var attrName = attr.substring(0, attr.indexOf('='));
var attrValue = attr.substring(attr.indexOf('"') + 1, attr.lastIndexOf('"'));
newTag += "<" + attrName + ">" + attrValue + "</" + attrName + ">";
}
}
xmlStr = xmlStr.replace(oldTag, newTag);
}
}
return xmlStr;
}