login  Naam:   Wachtwoord: 
Registreer je!
 Forum

Hoe de volgende .js te gebruiken om een url in te lezen?

Offline Waryaghar - 30/06/2012 15:49
Avatar van WaryagharLid Hallo,

Ik heb de volgende .js code waarmee ik een json url wil invoeren en dat deze dan omgezet wordt naar xml. Dit invoeren en omzetten, hoe kan ik dat doen door simpelweg alleen een link in het adressenbalk te gebruiken die de omzetting naar xml weergeeft? (Het doel is om het geschikt te maken als rss feed.)

Dit is de inhoud van het .js bestand:

  1. /**
  2.  * JSON to XML jQuery plugin. Provides quick way to convert JSON object to XML
  3.  * string. To some extent, allows control over XML output.
  4.  * Just as jQuery itself, this plugin is released under both MIT & GPL licences.
  5.  *
  6.  * @version 1.02
  7.  * @author Micha³ Korecki, www.michalkorecki.com
  8.  */
  9. (function($) {
  10. /**
  11. * Converts JSON object to XML string.
  12. *
  13. * @param json object to convert
  14. * @param options additional parameters
  15. * @return XML string
  16. */
  17. $.json2xml = function(json, options) {
  18. settings = {};
  19. settings = $.extend(true, settings, defaultSettings, options || { });
  20. return convertToXml(json, settings.rootTagName, '', 0);
  21. };
  22.  
  23. var defaultSettings = {
  24. formatOutput: false,
  25. formatTextNodes: false,
  26. indentString: ' ',
  27. rootTagName: 'root',
  28. ignore: [],
  29. replace: [],
  30. nodes: [],
  31. ///TODO: exceptions system
  32. exceptions: []
  33. };
  34.  
  35. /**
  36. * This is actual settings object used throught plugin, default settings
  37. * are stored separately to prevent overriding when using multiple times.
  38. */
  39. var settings = {};
  40.  
  41. /**
  42. * Core function parsing JSON to XML. It iterates over object properties and
  43. * creates XML attributes appended to main tag, if property is primitive
  44. * value (eg. string, number).
  45. * Otherwise, if it's array or object, new node is created and appened to
  46. * parent tag.
  47. * You can alter this behaviour by providing values in settings.ignore,
  48. * settings.replace and settings.nodes arrays.
  49. *
  50. * @param json object to parse
  51. * @param tagName name of tag created for parsed object
  52. * @param parentPath path to properly identify elements in ignore, replace
  53. * and nodes arrays
  54. * @param depth current element's depth
  55. * @return XML string
  56. */
  57. var convertToXml = function(json, tagName, parentPath, depth) {
  58. var suffix = (settings.formatOutput) ? '\r\n' : '';
  59. var indent = (settings.formatOutput) ? getIndent(depth) : '';
  60. var xmlTag = indent + '<' + tagName;
  61. var children = '';
  62.  
  63. for (var key in json) {
  64. if (json.hasOwnProperty(key)) {
  65. var propertyPath = parentPath + key;
  66. var propertyName = getPropertyName(parentPath, key);
  67. // element not in ignore array, process
  68. if ($.inArray(propertyPath, settings.ignore) == -1) {
  69. // array, create new child element
  70. if ($.isArray(json[key])) {
  71. children += createNodeFromArray(json[key], propertyName,
  72. propertyPath + '.', depth + 1, suffix);
  73. }
  74. // object, new child element aswell
  75. else if (typeof(json[key]) === 'object') {
  76. children += convertToXml(json[key], propertyName,
  77. propertyPath + '.', depth + 1);
  78. }
  79. // primitive value property as attribute
  80. else {
  81. // unless it's explicitly defined it should be node
  82. if ($.inArray(propertyPath, settings.nodes) != -1) {
  83. children += createTextNode(propertyName, json[key],
  84. depth, suffix);
  85. }
  86. else {
  87. xmlTag += ' ' + propertyName + '="' + json[key] + '"';
  88. }
  89. }
  90. }
  91. }
  92. }
  93. // close tag properly
  94. if (children !== '') {
  95. xmlTag += '>' + suffix + children + indent + '</' + tagName + '>' + suffix;
  96. }
  97. else {
  98. xmlTag += '/>' + suffix;
  99. }
  100. return xmlTag;
  101. };
  102.  
  103.  
  104. /**
  105. * Creates indent string for provided depth value. See settings for details.
  106. *
  107. * @param depth
  108. * @return indent string
  109. */
  110. var getIndent = function(depth) {
  111. var output = '';
  112. for (var i = 0; i < depth; i++) {
  113. output += settings.indentString;
  114. }
  115. return output;
  116. };
  117.  
  118.  
  119. /**
  120. * Checks settings.replace array for provided name, if it exists returns
  121. * replacement name. Else, original name is returned.
  122. *
  123. * @param parentPath path to this element's parent
  124. * @param name name of element to look up
  125. * @return element's final name
  126. */
  127. var getPropertyName = function(parentPath, name) {
  128. var index = settings.replace.length;
  129. var searchName = parentPath + name;
  130. while (index--) {
  131. // settings.replace array consists of {original : replacement}
  132. // objects
  133. if (settings.replace[index].hasOwnProperty(searchName)) {
  134. return settings.replace[index][searchName];
  135. }
  136. }
  137. return name;
  138. };
  139.  
  140. /**
  141. * Creates XML node from javascript array object.
  142. *
  143. * @param source
  144. * @param name XML element name
  145. * @param path parent element path string
  146. * @param depth
  147. * @param suffix node suffix (whether to format output or not)
  148. * @return XML tag string for provided array
  149. */
  150. var createNodeFromArray = function(source, name, path, depth, suffix) {
  151. var xmlNode = '';
  152. if (source.length > 0) {
  153. for (var index in source) {
  154. // array's element isn't object - it's primitive value, which
  155. // means array might need to be converted to text nodes
  156. if (typeof(source[index]) !== 'object') {
  157. // empty strings will be converted to empty nodes
  158. if (source[index] === "") {
  159. xmlNode += getIndent(depth) + '<' + name + '/>' + suffix;
  160. }
  161. else {
  162. var textPrefix = (settings.formatTextNodes)
  163. ? suffix + getIndent(depth + 1) : '';
  164. var textSuffix = (settings.formatTextNodes)
  165. ? suffix + getIndent(depth) : '';
  166. xmlNode += getIndent(depth) + '<' + name + '>'
  167. + textPrefix + source[index] + textSuffix
  168. + '</' + name + '>' + suffix;
  169. }
  170. }
  171. // else regular conversion applies
  172. else {
  173. xmlNode += convertToXml(source[index], name, path, depth);
  174. }
  175. }
  176. }
  177. // array is empty, also creating empty XML node
  178. else {
  179. xmlNode += getIndent(depth) + '<' + name + '/>' + suffix;
  180. }
  181. return xmlNode;
  182. };
  183.  
  184. /**
  185. * Creates node containing text only.
  186. *
  187. * @param name node's name
  188. * @param text node text string
  189. * @param parentDepth this node's parent element depth
  190. * @param suffix node suffix (whether to format output or not)
  191. * @return XML tag string
  192. */
  193. var createTextNode = function(name, text, parentDepth, suffix) {
  194. // unformatted text node: <node>value</node>
  195. // formatting includes value indentation and new lines
  196. var textPrefix = (settings.formatTextNodes)
  197. ? suffix + getIndent(parentDepth + 2) : '';
  198. var textSuffix = (settings.formatTextNodes)
  199. ? suffix + getIndent(parentDepth + 1) : '';
  200. var xmlNode = getIndent(parentDepth + 1) + '<' + name + '>'
  201. + textPrefix + text + textSuffix
  202. + '</' + name + '>' + suffix;
  203. return xmlNode;
  204. };
  205. })(jQuery);

1 antwoord

Gesponsorde links
Offline Martijn - 02/07/2012 13:13
Avatar van Martijn Crew PHP Hallo Waryaghar, ik begrijp je vraag niet helemaal, mogelijk kun je dit proberen beter te verwoorden. Eventueel met een simpel voorbeeldje.

Zou je ook je code even willen verplaatsen naar plaatscode.be en de link hier plaatsen? Dat maakt je topic een stuk overzichtelijker 
Gesponsorde links
Je moet ingelogd zijn om een reactie te kunnen posten.
Actieve forumberichten
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.171s