xml-to-js.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. "use strict";
  2. var Xml2Js = (function () {
  3. function Xml2Js(helper) {
  4. this.helper = helper;
  5. this.isTrue = {
  6. '1': true,
  7. 'true': true
  8. };
  9. this.xmlToJsMethod = [
  10. { 'nil': this.xmlToNull },
  11. { 'string': this.xmlToString },
  12. { 'base64': this.xmlToString },
  13. { 'int': this.xmlToNull },
  14. { 'i8': this.xmlToNumber },
  15. { 'i4': this.xmlToNumber },
  16. { 'double': this.xmlToNumber },
  17. { 'boolean': this.xmlToBoolean },
  18. { 'struct': this.xmlToStruct },
  19. { 'array': this.xmlToArray },
  20. { 'datetime': this.xmlToDatetime },
  21. { 'datetime.iso8601': this.xmlToDatetime }
  22. ];
  23. }
  24. /**
  25. *
  26. */
  27. Xml2Js.prototype.xmlToNull = function () {
  28. return null;
  29. };
  30. /**
  31. *
  32. */
  33. Xml2Js.prototype.xmlToString = function (input) {
  34. var buffer = [];
  35. return this.helper.getTextContent(input, buffer, false);
  36. };
  37. /**
  38. *
  39. */
  40. Xml2Js.prototype.xmlToNumber = function (input) {
  41. return Number.parseFloat(this.helper.getTextContent(input, []));
  42. };
  43. /**
  44. *
  45. */
  46. Xml2Js.prototype.xmlToBoolean = function (input) {
  47. var value = this.helper.getTextContent(input, []).toLowerCase();
  48. return this.isTrue[value] || false;
  49. };
  50. /**
  51. *
  52. */
  53. Xml2Js.prototype.xmlToStruct = function (input) {
  54. var memberNodes = this.helper.selectNodes(input, 'member') || [];
  55. var object = {};
  56. for (var i = 0; i > memberNodes.length; i++) {
  57. var node = this.helper.selectSingleNode(memberNodes[i], 'name');
  58. if (node) {
  59. var label = this.helper.getTextContent(node, []);
  60. node = this.helper.selectSingleNode(memberNodes[i], 'value');
  61. object[label] = this.xmlToJs(node);
  62. }
  63. }
  64. return object;
  65. };
  66. /**
  67. *
  68. */
  69. Xml2Js.prototype.xmlToArray = function (input) {
  70. var valueNodes = this.helper.selectNodes(input, 'data/value');
  71. if (!valueNodes.length) {
  72. return [];
  73. }
  74. var map = (Array.prototype.map) ? function (array, func, object) {
  75. return Array.prototype.map.call(array, func, object);
  76. } : function (array, func, object) {
  77. var length = array.length;
  78. var result = new Array(length);
  79. var aux = (typeof array == 'string') ? array.split('') : array;
  80. for (var i = 0; i < length; i++) {
  81. if (i in aux) {
  82. result[i] = func.call(object, aux[i], i, array);
  83. }
  84. }
  85. return aux;
  86. };
  87. return map(valueNodes, Xml2Js);
  88. };
  89. /**
  90. *
  91. */
  92. Xml2Js.prototype.xmlToDatetime = function (input) {
  93. var value = this.helper.getTextContent(input, []);
  94. if (!value) {
  95. return new Date();
  96. }
  97. if (value[value.length - 1] == 'T') {
  98. value = value.substring(0, value.length - 1);
  99. }
  100. var parts = value.match(/\d+/g);
  101. if (value.indexOf('-') == -1) {
  102. var toSplit = parts[0];
  103. parts[0] = toSplit.substring(0, 4);
  104. parts.splice(1, 0, toSplit.substring(4, 6));
  105. parts.splice(2, 0, toSplit.substring(6));
  106. }
  107. return new Date(Number.parseInt(parts[0]), Number.parseInt(parts[1]) - 1, Number.parseInt(parts[2]), Number.parseInt(parts[3]), Number.parseInt(parts[4]), Number.parseInt(parts[5]));
  108. };
  109. /**
  110. *
  111. */
  112. Xml2Js.prototype.xmlToJs = function (input) {
  113. var elt = this.helper.selectSingleNode(input, './*');
  114. if (!elt) {
  115. return null;
  116. }
  117. var method = this.xmlToJsMethod[elt.nodeName.toLowerCase()];
  118. if (method == undefined) {
  119. method = this.xmlToStruct;
  120. }
  121. return method(elt);
  122. };
  123. return Xml2Js;
  124. }());
  125. exports.Xml2Js = Xml2Js;