-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-xml-to-json.js
60 lines (49 loc) · 1.76 KB
/
convert-xml-to-json.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
function convertHyphenToUnderscore(str) {
return str.split("-").join("_");
}
function parseElement(xml) {
var data = {};
// if it's text just return it
if (xml.nodeType === 3) {
return xml.nodeValue.trim();
}
// if it doesn't have any children or attributes, just return the contents
if ((!xml.children || !xml.children.length) && (!xml.attributes || !xml.attributes.length)) {
return xml.textContent;
}
// if it doesn't have children but does have body content, add it to text
if ((!xml.children || !xml.children.length) && xml.textContent) {
data.text = xml.textContent;
}
// if it's an element with attributes, add them to data.attributes
if (xml.nodeType === 1 && xml.attributes && xml.attributes.length > 0) {
data.attributes = _.reduce(xml.attributes, function(obj, name, id) {
var attr = xml.attributes.item(id);
obj[convertHyphenToUnderscore(attr.name)] = attr.value;
return obj;
}, {});
}
// recursively call parseElement over children
_.each(xml.children, function(child) {
var name = convertHyphenToUnderscore(child.nodeName);
// if name not in data, add and return
if (!_.has(data, name)) {
data[name] = parseElement(child);
return;
}
// if name already in data, but not as an array, make it array
if (!_.isArray(data[name])) {
data[name] = [data[name]];
}
// now push the new child
data[name].push(parseElement(child));
});
return data;
}
// load XML
var xmlText = getXML();
// use the DOMParser browser API to convert string to a Document
var XML = new DOMParser().parseFromString(xmlText, "text/xml");
var obj = parseElement(XML);
document.getElementById("result").innerHTML = JSON.stringify(obj);
console.log("JSON Object : ", JSON.stringify(obj));