|
| 1 | +/** |
| 2 | + * jQuery plugin to convert a given $.ajax response xml object to json. |
| 3 | + * |
| 4 | + * @example var json = $.xml2json(response); |
| 5 | + */ |
| 6 | +(function() { |
| 7 | + |
| 8 | + // default options based on https://github.com/Leonidas-from-XIV/node-xml2js |
| 9 | + var defaultOptions = { |
| 10 | + attrkey: '$', |
| 11 | + charkey: '_' |
| 12 | + }; |
| 13 | + |
| 14 | + function parseXML(data) { |
| 15 | + var xml, tmp; |
| 16 | + if (!data || typeof data !== "string") { |
| 17 | + return null; |
| 18 | + } |
| 19 | + try { |
| 20 | + if (window.DOMParser) { // Standard |
| 21 | + tmp = new DOMParser(); |
| 22 | + xml = tmp.parseFromString(data, "text/xml"); |
| 23 | + } else { // IE |
| 24 | + xml = new ActiveXObject("Microsoft.XMLDOM"); |
| 25 | + xml.async = "false"; |
| 26 | + xml.loadXML(data); |
| 27 | + } |
| 28 | + } catch (e) { |
| 29 | + xml = undefined; |
| 30 | + } |
| 31 | + if (!xml || !xml.documentElement || xml.getElementsByTagName("parsererror").length) { |
| 32 | + throw new Error("Invalid XML: " + data); |
| 33 | + } |
| 34 | + return xml; |
| 35 | + } |
| 36 | + |
| 37 | + /**w |
| 38 | + * Converts an xml response object from a $.ajax() call to a JSON object. |
| 39 | + * |
| 40 | + * @param xml |
| 41 | + */ |
| 42 | + function xml2json(xml, options) { |
| 43 | + options = options || defaultOptions; |
| 44 | + |
| 45 | + if (typeof xml === 'string') { |
| 46 | + xml = parseXML(xml); |
| 47 | + } |
| 48 | + |
| 49 | + var result = {}; |
| 50 | + |
| 51 | + for (var i in xml.childNodes) { |
| 52 | + var node = xml.childNodes[i]; |
| 53 | + if (node.nodeType === 1) { |
| 54 | + var child = node.hasChildNodes() ? xml2json(node, options) : node.nodevalue; |
| 55 | + child = child === null ? {} : child; |
| 56 | + |
| 57 | + if (result.hasOwnProperty(node.nodeName)) { |
| 58 | + // For repeating elements, cast/promote the node to array |
| 59 | + if (!(result[node.nodeName] instanceof Array)) { |
| 60 | + var tmp = result[node.nodeName]; |
| 61 | + result[node.nodeName] = []; |
| 62 | + result[node.nodeName].push(tmp); |
| 63 | + } |
| 64 | + result[node.nodeName].push(child); |
| 65 | + } else { |
| 66 | + result[node.nodeName] = child; |
| 67 | + } |
| 68 | + |
| 69 | + // Add attributes if any |
| 70 | + if (node.attributes.length > 0) { |
| 71 | + result[node.nodeName][options.attrkey] = {}; |
| 72 | + child[options.attrkey] = {}; |
| 73 | + for (var j in node.attributes) { |
| 74 | + var attribute = node.attributes.item(j); |
| 75 | + child[options.attrkey][attribute.nodeName] = attribute.value; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Add element value |
| 80 | + if (node.childElementCount === 0 && node.textContent !== null && node.textContent !== "") { |
| 81 | + child[options.charkey] = node.textContent.trim(); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + if (typeof jQuery !== 'undefined') { |
| 90 | + jQuery.extend({xml2json: xml2json}); |
| 91 | + } else if (typeof module !== 'undefined') { |
| 92 | + module.exports = xml2json; |
| 93 | + } else if (typeof window !== 'undefined') { |
| 94 | + window.xml2json = xml2json; |
| 95 | + } |
| 96 | +})(); |
0 commit comments