json2xml.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //copyright Ryan Day 2010 <http://ryanday.org>, Joscha Feth 2013 <http://www.feth.com> [MIT Licensed]
  2. var element_start_char =
  3. "a-zA-Z_\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FFF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD";
  4. var element_non_start_char = "\-.0-9\u00B7\u0300-\u036F\u203F\u2040";
  5. var element_replace = new RegExp("^([^" + element_start_char + "])|^((x|X)(m|M)(l|L))|([^" + element_start_char + element_non_start_char + "])", "g");
  6. var not_safe_in_xml = /[^\x09\x0A\x0D\x20-\xFF\x85\xA0-\uD7FF\uE000-\uFDCF\uFDE0-\uFFFD]/gm;
  7. var objKeys = function (obj) {
  8. var l = [];
  9. if (obj instanceof Object) {
  10. for (var k in obj) {
  11. if (obj.hasOwnProperty(k)) {
  12. l.push(k);
  13. }
  14. }
  15. }
  16. return l;
  17. };
  18. var process_to_xml = function (node_data, options) {
  19. var makeNode = function (name, content, attributes, level, hasSubNodes) {
  20. var indent_value = options.indent !== undefined ? options.indent : "\t";
  21. var indent = options.prettyPrint ? '\n' + new Array(level).join(indent_value) : '';
  22. if (options.removeIllegalNameCharacters) {
  23. name = name.replace(element_replace, '_');
  24. }
  25. var node = [indent, '<', name, (attributes || '')];
  26. if (content && content.length > 0) {
  27. node.push('>')
  28. node.push(content);
  29. hasSubNodes && node.push(indent);
  30. node.push('</');
  31. node.push(name);
  32. node.push('>');
  33. } else {
  34. node.push('/>');
  35. }
  36. return node.join('');
  37. };
  38. return (function fn(node_data, node_descriptor, level) {
  39. var type = typeof node_data;
  40. if ((Array.isArray) ? Array.isArray(node_data) : node_data instanceof Array) {
  41. type = 'array';
  42. } else if (node_data instanceof Date) {
  43. type = 'date';
  44. }
  45. switch (type) {
  46. //if value is an array create child nodes from values
  47. case 'array':
  48. var ret = [];
  49. node_data.map(function (v) {
  50. ret.push(fn(v, 1, level + 1));
  51. //entries that are values of an array are the only ones that can be special node descriptors
  52. });
  53. options.prettyPrint && ret.push('\n');
  54. return ret.join('');
  55. break;
  56. case 'date':
  57. // cast dates to ISO 8601 date (soap likes it)
  58. return node_data.toJSON ? node_data.toJSON() : node_data + '';
  59. break;
  60. case 'object':
  61. var nodes = [];
  62. for (var name in node_data) {
  63. if (node_data[name] instanceof Array) {
  64. for (var j in node_data[name]) {
  65. nodes.push(makeNode(name, fn(node_data[name][j], 0, level + 1), null, level + 1, objKeys(node_data[name][j]).length));
  66. }
  67. } else {
  68. nodes.push(makeNode(name, fn(node_data[name], 0, level + 1), null, level + 1));
  69. }
  70. }
  71. options.prettyPrint && nodes.length > 0 && nodes.push('\n');
  72. return nodes.join('');
  73. break;
  74. case 'function':
  75. return node_data();
  76. break;
  77. default:
  78. return options.escape ? esc(node_data) : '' + node_data;
  79. }
  80. }(node_data, 0, 0))
  81. };
  82. var xml_header = function (standalone) {
  83. var ret = ['<?xml version="1.0" encoding="UTF-8"'];
  84. if (standalone) {
  85. ret.push(' standalone="yes"');
  86. }
  87. ret.push('?>');
  88. return ret.join('');
  89. };
  90. function esc(str) {
  91. return ('' + str).replace(/&/g, '&amp;')
  92. .replace(/</g, '&lt;')
  93. .replace(/>/g, '&gt;')
  94. .replace(/'/g, '&apos;')
  95. .replace(/"/g, '&quot;')
  96. .replace(not_safe_in_xml, '');
  97. }
  98. var json2xml = function (obj, options) {
  99. if (!options) {
  100. options = {
  101. xmlHeader: {
  102. standalone: true
  103. },
  104. prettyPrint: true,
  105. indent: " "
  106. };
  107. }
  108. if (typeof obj == 'string') {
  109. try {
  110. obj = JSON.parse(obj.toString());
  111. } catch (e) {
  112. return false;
  113. }
  114. }
  115. var xmlheader = '';
  116. var docType = '';
  117. if (options) {
  118. if (typeof options == 'object') {
  119. // our config is an object
  120. if (options.xmlHeader) {
  121. // the user wants an xml header
  122. xmlheader = xml_header(!!options.xmlHeader.standalone);
  123. }
  124. if (typeof options.docType != 'undefined') {
  125. docType = '<!DOCTYPE ' + options.docType + '>'
  126. }
  127. } else {
  128. // our config is a boolean value, so just add xml header
  129. xmlheader = xml_header();
  130. }
  131. }
  132. options = options || {}
  133. var ret = [
  134. xmlheader,
  135. (options.prettyPrint && docType ? '\n' : ''),
  136. docType,
  137. process_to_xml(obj, options)
  138. ];
  139. return ret.join('').replace(/\n{2,}/g, '\n').replace(/\s+$/g, '');
  140. };
  141. module.exports = json2xml;