-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
83 lines (75 loc) · 2.59 KB
/
main.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
/**
* Copyright (C) 2012 Brien Coffield, [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* <http://www.gnu.org/licenses/gpl-3.0.html>.
*/
/**
* Converts xml DOM to json.
* Note: no attributes are converted.
* Result will contain only nodeNames and node.data where applicable.
* Also skips all whitespace.
*/
xmlToJson = function(xml, json) {
if (json === undefined) {
// Should only happen on the first call.
json = {};
}
if (typeof(xml) === "string") {
// Typically you want to pass xml that has already been parsed.
// From an xmlhttpresponse, use xmlhttpresponse.responseXML instead
// of responseText to skip this branch.
xml = (new DOMParser()).parseFromString(xml, "text/xml");
}
var i = 0;
for (; i < xml.childNodes.length; i++) {
var childNode = xml.childNodes[i];
if (childNode.data !== undefined) {
if (childNode.data.trim() !== "") {
json.data = childNode.data;
}
continue;
}
var dataStructure = {};
if (json[childNode.nodeName] === undefined) {
// Node has not been seen before. Pass empty object as json.
json[childNode.nodeName] = dataStructure;
}
else if (json[childNode.nodeName].length === undefined) {
// Node has been seen once before. Convert to array.
var old = json[childNode.nodeName];
json[childNode.nodeName] = [];
json[childNode.nodeName].push(old);
json[childNode.nodeName].push(dataStructure);
}
else {
// Node has been seen at least twice now. Just push new object to array.
json[childNode.nodeName].push(dataStructure);
}
// Mutate json recursively.
xmlToJson(childNode, dataStructure);
}
// All mutations finished, return result.
return json;
};
fakeData = function() {
var xml = "<root>";
xml += "<events>";
xml += "<event>";
xml += "event1";
xml += "</event>";
xml += "<event>";
xml += "event2";
xml += "</event>";
xml += "</events>";
xml += "</root>";
return xml;
};
console.log(xmlToJson(fakeData()));