xml2json.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // default options based on https://github.com/Leonidas-from-XIV/node-xml2js
  8. var defaultOptions = {
  9. attrkey: '$',
  10. charkey: '_',
  11. normalize: false,
  12. explicitArray: false
  13. };
  14. // extracted from jquery
  15. function parseXML(data) {
  16. var xml, tmp;
  17. if (!data || typeof data !== "string") {
  18. return null;
  19. }
  20. try {
  21. if (window.DOMParser) { // Standard
  22. tmp = new DOMParser();
  23. xml = tmp.parseFromString(data, "text/xml");
  24. } else { // IE
  25. xml = new ActiveXObject("Microsoft.XMLDOM");
  26. xml.async = "false";
  27. xml.loadXML(data);
  28. }
  29. } catch (e) {
  30. xml = undefined;
  31. }
  32. if (!xml || !xml.documentElement || xml.getElementsByTagName("parsererror").length) {
  33. throw new Error("Invalid XML: " + data);
  34. }
  35. return xml;
  36. }
  37. function normalize(value, options){
  38. if (!!options.normalize){
  39. return (value || '').trim();
  40. }
  41. return value;
  42. }
  43. function xml2jsonImpl(xml, options) {
  44. var i, result = {}, attrs = {}, node, child, name;
  45. result[options.attrkey] = attrs;
  46. if (xml.attributes && xml.attributes.length > 0) {
  47. for (i = 0; i < xml.attributes.length; i++){
  48. var item = xml.attributes.item(i);
  49. attrs[item.nodeName] = item.value;
  50. }
  51. }
  52. // element content
  53. if (xml.childElementCount === 0) {
  54. result[options.charkey] = normalize(xml.textContent, options);
  55. }
  56. for (i = 0; i < xml.childNodes.length; i++) {
  57. node = xml.childNodes[i];
  58. if (node.nodeName === "#comment")
  59. continue;
  60. if (node.nodeType === 1) {
  61. if (node.attributes.length === 0 && node.childElementCount === 0){
  62. child = normalize(node.textContent, options);
  63. } else {
  64. child = xml2jsonImpl(node, options);
  65. }
  66. name = node.nodeName;
  67. if (result.hasOwnProperty(name)) {
  68. // For repeating elements, cast/promote the node to array
  69. var val = result[name];
  70. if (!Array.isArray(val)) {
  71. val = [val];
  72. result[name] = val;
  73. }
  74. val.push(child);
  75. } else if(options.explicitArray === true) {
  76. result[name] = [child];
  77. } else {
  78. result[name] = child;
  79. }
  80. }
  81. }
  82. return result;
  83. }
  84. /**w
  85. * Converts an xml document or string to a JSON object.
  86. *
  87. * @param xml
  88. */
  89. function xml2json(xml, options) {
  90. var n;
  91. if (!xml) {
  92. return xml;
  93. }
  94. options = options || {};
  95. for(n in defaultOptions) {
  96. if(defaultOptions.hasOwnProperty(n) && options[n] === undefined) {
  97. options[n] = defaultOptions[n];
  98. }
  99. }
  100. if (typeof xml === 'string') {
  101. xml = parseXML(xml).documentElement;
  102. }
  103. var root = {};
  104. if (xml.attributes && xml.attributes.length === 0 && xml.childElementCount === 0){
  105. root[xml.nodeName] = normalize(xml.textContent, options);
  106. } else {
  107. root[xml.nodeName] = xml2jsonImpl(xml, options);
  108. }
  109. return root;
  110. }
  111. if (typeof jQuery !== 'undefined') {
  112. jQuery.extend({xml2json: xml2json});
  113. } else if (typeof module !== 'undefined') {
  114. module.exports = xml2json;
  115. } else if (typeof window !== 'undefined') {
  116. window.xml2json = xml2json;
  117. }
  118. })();