|
3 | 3 | * |
4 | 4 | * @example var json = $.xml2json(response); |
5 | 5 | */ |
6 | | -(function(){ |
7 | | - jQuery.extend({ |
| 6 | +(function() { |
| 7 | + jQuery.extend({ |
8 | 8 |
|
9 | | - /**w |
10 | | - * Converts an xml response object from a $.ajax() call to a JSON object. |
11 | | - * |
12 | | - * @param xml |
13 | | - */ |
14 | | - xml2json: function xml2json(xml) { |
15 | | - var result = {}; |
| 9 | + /**w |
| 10 | + * Converts an xml response object from a $.ajax() call to a JSON object. |
| 11 | + * |
| 12 | + * @param xml |
| 13 | + */ |
| 14 | + xml2json: function xml2json(xml) { |
| 15 | + var result = {}; |
16 | 16 |
|
17 | | - for(var i in xml.childNodes) { |
18 | | - var node = xml.childNodes[i]; |
19 | | - if(node.nodeType == 1) { |
20 | | - var child = node.hasChildNodes() ? xml2json(node) : node.nodevalue; |
21 | | - child = child == null ? {} : child; |
| 17 | + for (var i in xml.childNodes) { |
| 18 | + var node = xml.childNodes[i]; |
| 19 | + if (node.nodeType === 1) { |
| 20 | + var child = node.hasChildNodes() ? xml2json(node) : node.nodevalue; |
| 21 | + child = child === null ? {} : child; |
22 | 22 |
|
23 | | - if(result.hasOwnProperty(node.nodeName)) { |
24 | | - // For repeating elements, cast/promote the node to array |
25 | | - if(!(result[node.nodeName] instanceof Array)){ |
26 | | - var tmp = result[node.nodeName]; |
27 | | - result[node.nodeName] = []; |
28 | | - result[node.nodeName].push(tmp); |
29 | | - } |
30 | | - result[node.nodeName].push(child); |
31 | | - } else { |
32 | | - result[node.nodeName] = child; |
33 | | - } |
| 23 | + if (result.hasOwnProperty(node.nodeName)) { |
| 24 | + // For repeating elements, cast/promote the node to array |
| 25 | + if (!(result[node.nodeName] instanceof Array)) { |
| 26 | + var tmp = result[node.nodeName]; |
| 27 | + result[node.nodeName] = []; |
| 28 | + result[node.nodeName].push(tmp); |
| 29 | + } |
| 30 | + result[node.nodeName].push(child); |
| 31 | + } else { |
| 32 | + result[node.nodeName] = child; |
| 33 | + } |
34 | 34 |
|
35 | | - // Add attributes if any |
36 | | - if(node.attributes.length > 0) { |
37 | | - result[node.nodeName]['@attributes'] = {}; |
38 | | - child['@attributes'] = {}; |
39 | | - for(var j in node.attributes) { |
40 | | - var attribute = node.attributes.item(j); |
41 | | - child['@attributes'][attribute.nodeName] = attribute.value; |
42 | | - } |
43 | | - } |
| 35 | + // Add attributes if any |
| 36 | + if (node.attributes.length > 0) { |
| 37 | + result[node.nodeName]['@attributes'] = {}; |
| 38 | + child['@attributes'] = {}; |
| 39 | + for (var j in node.attributes) { |
| 40 | + var attribute = node.attributes.item(j); |
| 41 | + child['@attributes'][attribute.nodeName] = attribute.value; |
| 42 | + } |
| 43 | + } |
44 | 44 |
|
45 | | - // Add element value |
46 | | - if(node.childElementCount == 0 && node.textContent != null && node.textContent != "") { |
47 | | - child.value = node.textContent.trim(); |
48 | | - } |
49 | | - } |
50 | | - } |
| 45 | + // Add element value |
| 46 | + if (node.childElementCount === 0 && node.textContent !== null && node.textContent !== "") { |
| 47 | + child.value = node.textContent.trim(); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
51 | 51 |
|
52 | | - return result; |
53 | | - } |
| 52 | + return result; |
| 53 | + } |
54 | 54 |
|
55 | | - }); |
| 55 | + }); |
56 | 56 | })(); |
0 commit comments