Xml2.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-data-reader-Xml'>/**
  19. </span> * @author Ed Spencer
  20. *
  21. * The XML Reader is used by a Proxy to read a server response that is sent back in XML format. This usually happens as
  22. * a result of loading a Store - for example we might create something like this:
  23. *
  24. * Ext.define('User', {
  25. * extend: 'Ext.data.Model',
  26. * fields: ['id', 'name', 'email']
  27. * });
  28. *
  29. * var store = Ext.create('Ext.data.Store', {
  30. * model: 'User',
  31. * proxy: {
  32. * type: 'ajax',
  33. * url : 'users.xml',
  34. * reader: {
  35. * type: 'xml',
  36. * record: 'user',
  37. * root: 'users'
  38. * }
  39. * }
  40. * });
  41. *
  42. * The example above creates a 'User' model. Models are explained in the {@link Ext.data.Model Model} docs if you're not
  43. * already familiar with them.
  44. *
  45. * We created the simplest type of XML Reader possible by simply telling our {@link Ext.data.Store Store}'s {@link
  46. * Ext.data.proxy.Proxy Proxy} that we want a XML Reader. The Store automatically passes the configured model to the
  47. * Store, so it is as if we passed this instead:
  48. *
  49. * reader: {
  50. * type : 'xml',
  51. * model: 'User',
  52. * record: 'user',
  53. * root: 'users'
  54. * }
  55. *
  56. * The reader we set up is ready to read data from our server - at the moment it will accept a response like this:
  57. *
  58. * &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  59. * &lt;users&gt;
  60. * &lt;user&gt;
  61. * &lt;id&gt;1&lt;/id&gt;
  62. * &lt;name&gt;Ed Spencer&lt;/name&gt;
  63. * &lt;email&gt;ed@sencha.com&lt;/email&gt;
  64. * &lt;/user&gt;
  65. * &lt;user&gt;
  66. * &lt;id&gt;2&lt;/id&gt;
  67. * &lt;name&gt;Abe Elias&lt;/name&gt;
  68. * &lt;email&gt;abe@sencha.com&lt;/email&gt;
  69. * &lt;/user&gt;
  70. * &lt;/users&gt;
  71. *
  72. * First off there's {@link #root} option to define the root node `&lt;users&gt;` (there should be only one in a well-formed
  73. * XML document). Then the XML Reader uses the configured {@link #record} option to pull out the data for each record -
  74. * in this case we set record to 'user', so each `&lt;user&gt;` above will be converted into a User model.
  75. *
  76. * Note that XmlReader doesn't care whether your {@link #root} and {@link #record} elements are nested deep inside a
  77. * larger structure, so a response like this will still work:
  78. *
  79. * &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  80. * &lt;deeply&gt;
  81. * &lt;nested&gt;
  82. * &lt;xml&gt;
  83. * &lt;users&gt;
  84. * &lt;user&gt;
  85. * &lt;id&gt;1&lt;/id&gt;
  86. * &lt;name&gt;Ed Spencer&lt;/name&gt;
  87. * &lt;email&gt;ed@sencha.com&lt;/email&gt;
  88. * &lt;/user&gt;
  89. * &lt;user&gt;
  90. * &lt;id&gt;2&lt;/id&gt;
  91. * &lt;name&gt;Abe Elias&lt;/name&gt;
  92. * &lt;email&gt;abe@sencha.com&lt;/email&gt;
  93. * &lt;/user&gt;
  94. * &lt;/users&gt;
  95. * &lt;/xml&gt;
  96. * &lt;/nested&gt;
  97. * &lt;/deeply&gt;
  98. *
  99. * # Response metadata
  100. *
  101. * The server can return additional data in its response, such as the {@link #totalProperty total number of records} and
  102. * the {@link #successProperty success status of the response}. These are typically included in the XML response like
  103. * this:
  104. *
  105. * &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  106. * &lt;users&gt;
  107. * &lt;total&gt;100&lt;/total&gt;
  108. * &lt;success&gt;true&lt;/success&gt;
  109. * &lt;user&gt;
  110. * &lt;id&gt;1&lt;/id&gt;
  111. * &lt;name&gt;Ed Spencer&lt;/name&gt;
  112. * &lt;email&gt;ed@sencha.com&lt;/email&gt;
  113. * &lt;/user&gt;
  114. * &lt;user&gt;
  115. * &lt;id&gt;2&lt;/id&gt;
  116. * &lt;name&gt;Abe Elias&lt;/name&gt;
  117. * &lt;email&gt;abe@sencha.com&lt;/email&gt;
  118. * &lt;/user&gt;
  119. * &lt;/users&gt;
  120. *
  121. * If these properties are present in the XML response they can be parsed out by the XmlReader and used by the Store
  122. * that loaded it. We can set up the names of these properties by specifying a final pair of configuration options:
  123. *
  124. * reader: {
  125. * type: 'xml',
  126. * root: 'users',
  127. * totalProperty : 'total',
  128. * successProperty: 'success'
  129. * }
  130. *
  131. * These final options are not necessary to make the Reader work, but can be useful when the server needs to report an
  132. * error or if it needs to indicate that there is a lot of data available of which only a subset is currently being
  133. * returned.
  134. *
  135. * # Response format
  136. *
  137. * **Note:** in order for the browser to parse a returned XML document, the Content-Type header in the HTTP response
  138. * must be set to &quot;text/xml&quot; or &quot;application/xml&quot;. This is very important - the XmlReader will not work correctly
  139. * otherwise.
  140. */
  141. Ext.define('Ext.data.reader.Xml', {
  142. extend: 'Ext.data.reader.Reader',
  143. alternateClassName: 'Ext.data.XmlReader',
  144. alias : 'reader.xml',
  145. <span id='Ext-data-reader-Xml-cfg-record'> /**
  146. </span> * @cfg {String} record (required)
  147. * The DomQuery path to the repeated element which contains record information.
  148. */
  149. <span id='Ext-data-reader-Xml-method-createAccessor'> /**
  150. </span> * @private
  151. * Creates a function to return some particular key of data from a response. The totalProperty and
  152. * successProperty are treated as special cases for type casting, everything else is just a simple selector.
  153. * @param {String} key
  154. * @return {Function}
  155. */
  156. createAccessor: function(expr) {
  157. var me = this;
  158. if (Ext.isEmpty(expr)) {
  159. return Ext.emptyFn;
  160. }
  161. if (Ext.isFunction(expr)) {
  162. return expr;
  163. }
  164. return function(root) {
  165. return me.getNodeValue(Ext.DomQuery.selectNode(expr, root));
  166. };
  167. },
  168. getNodeValue: function(node) {
  169. if (node &amp;&amp; node.firstChild) {
  170. return node.firstChild.nodeValue;
  171. }
  172. return undefined;
  173. },
  174. //inherit docs
  175. getResponseData: function(response) {
  176. var xml = response.responseXML,
  177. error,
  178. msg;
  179. if (!xml) {
  180. msg = 'XML data not found in the response';
  181. error = new Ext.data.ResultSet({
  182. total : 0,
  183. count : 0,
  184. records: [],
  185. success: false,
  186. message: msg
  187. });
  188. this.fireEvent('exception', this, response, error);
  189. Ext.Logger.warn(msg);
  190. return error;
  191. }
  192. return this.readRecords(xml);
  193. },
  194. <span id='Ext-data-reader-Xml-method-getData'> /**
  195. </span> * Normalizes the data object.
  196. * @param {Object} data The raw data object
  197. * @return {Object} The documentElement property of the data object if present, or the same object if not.
  198. */
  199. getData: function(data) {
  200. return data.documentElement || data;
  201. },
  202. <span id='Ext-data-reader-Xml-method-getRoot'> /**
  203. </span> * @private
  204. * Given an XML object, returns the Element that represents the root as configured by the Reader's meta data.
  205. * @param {Object} data The XML data object
  206. * @return {XMLElement} The root node element
  207. */
  208. getRoot: function(data) {
  209. var nodeName = data.nodeName,
  210. root = this.root;
  211. if (!root || (nodeName &amp;&amp; nodeName == root)) {
  212. return data;
  213. } else if (Ext.DomQuery.isXml(data)) {
  214. // This fix ensures we have XML data
  215. // Related to TreeStore calling getRoot with the root node, which isn't XML
  216. // Probably should be resolved in TreeStore at some point
  217. return Ext.DomQuery.selectNode(root, data);
  218. }
  219. },
  220. <span id='Ext-data-reader-Xml-method-extractData'> /**
  221. </span> * @private
  222. * We're just preparing the data for the superclass by pulling out the record nodes we want.
  223. * @param {XMLElement} root The XML root node
  224. * @return {Ext.data.Model[]} The records
  225. */
  226. extractData: function(root) {
  227. var recordName = this.record;
  228. //&lt;debug&gt;
  229. if (!recordName) {
  230. Ext.Error.raise('Record is a required parameter');
  231. }
  232. //&lt;/debug&gt;
  233. if (recordName != root.nodeName) {
  234. root = Ext.DomQuery.select(recordName, root);
  235. } else {
  236. root = [root];
  237. }
  238. return this.callParent([root]);
  239. },
  240. <span id='Ext-data-reader-Xml-method-getAssociatedDataRoot'> /**
  241. </span> * @private
  242. * See Ext.data.reader.Reader's getAssociatedDataRoot docs.
  243. * @param {Object} data The raw data object
  244. * @param {String} associationName The name of the association to get data for (uses associationKey if present)
  245. * @return {XMLElement} The root
  246. */
  247. getAssociatedDataRoot: function(data, associationName) {
  248. return Ext.DomQuery.select(associationName, data)[0];
  249. },
  250. <span id='Ext-data-reader-Xml-method-readRecords'> /**
  251. </span> * Parses an XML document and returns a ResultSet containing the model instances.
  252. * @param {Object} doc Parsed XML document
  253. * @return {Ext.data.ResultSet} The parsed result set
  254. */
  255. readRecords: function(doc) {
  256. // it's possible that we get passed an array here by associations.
  257. // Make sure we strip that out (see Ext.data.reader.Reader#readAssociated)
  258. if (Ext.isArray(doc)) {
  259. doc = doc[0];
  260. }
  261. <span id='Ext-data-reader-Xml-property-xmlData'> /**
  262. </span> * @property {Object} xmlData
  263. * Copy of {@link #rawData}.
  264. * @deprecated Will be removed in Ext JS 5.0. Use {@link #rawData} instead.
  265. */
  266. this.xmlData = doc;
  267. return this.callParent([doc]);
  268. },
  269. <span id='Ext-data-reader-Xml-method-createFieldAccessExpression'> /**
  270. </span> * @private
  271. * Returns an accessor expression for the passed Field from an XML element using either the Field's mapping, or
  272. * its ordinal position in the fields collsction as the index.
  273. * This is used by buildExtractors to create optimized on extractor function which converts raw data into model instances.
  274. */
  275. createFieldAccessExpression: function(field, fieldVarName, dataName) {
  276. var selector = field.mapping || field.name,
  277. result;
  278. if (typeof selector === 'function') {
  279. result = fieldVarName + '.mapping(' + dataName + ', this)';
  280. } else {
  281. result = 'me.getNodeValue(Ext.DomQuery.selectNode(&quot;' + selector + '&quot;, ' + dataName + '))';
  282. }
  283. return result;
  284. }
  285. });
  286. </pre>
  287. </body>
  288. </html>