5a11f8c6fc0dc4c3747a5a9be391d4b0443e79a2.svn-base 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /**
  7. * @class Ext.data.XmlReader
  8. * @extends Ext.data.DataReader Data reader class to create an Array of
  9. * {@link Ext.data.Record} objects from an XML document based on
  10. * mappings in a provided Ext.data.Record constructor.<br>
  11. * <br>
  12. * <p>
  13. * <em>Note that in order for the browser to parse a returned XML document, the Content-Type
  14. * header in the HTTP response must be set to "text/xml".</em>
  15. * <p>
  16. * Example code:
  17. *
  18. * <pre><code>
  19. * var Employee = Ext.data.Record.create([{
  20. * name : 'name',
  21. * mapping : 'name'
  22. * }, // &quot;mapping&quot; property not needed if it's the same as &quot;name&quot;
  23. * {
  24. * name : 'occupation'
  25. * } // This field will use &quot;occupation&quot; as the mapping.
  26. * ]);
  27. * var myReader = new Ext.data.XmlReader({
  28. * totalRecords : &quot;results&quot;, // The element which contains the total dataset size (optional)
  29. * record : &quot;row&quot;, // The repeated element which contains row information
  30. * id : &quot;id&quot; // The element within the row that provides an ID for the record (optional)
  31. * }, Employee);
  32. * </code></pre>
  33. *
  34. * <p>
  35. * This would consume an XML file like this:
  36. *
  37. * <pre><code>
  38. * &lt;?xml?&gt;
  39. * &lt;dataset&gt;
  40. * &lt;results&gt;2&lt;/results&gt;
  41. * &lt;row&gt;
  42. * &lt;id&gt;1&lt;/id&gt;
  43. * &lt;name&gt;Bill&lt;/name&gt;
  44. * &lt;occupation&gt;Gardener&lt;/occupation&gt;
  45. * &lt;/row&gt;
  46. * &lt;row&gt;
  47. * &lt;id&gt;2&lt;/id&gt;
  48. * &lt;name&gt;Ben&lt;/name&gt;
  49. * &lt;occupation&gt;Horticulturalist&lt;/occupation&gt;
  50. * &lt;/row&gt;
  51. * &lt;/dataset&gt;
  52. * </code></pre>
  53. *
  54. * @cfg {String} totalRecords The DomQuery path from which to retrieve the total
  55. * number of records in the dataset. This is only needed if the whole
  56. * dataset is not passed in one go, but is being paged from the remote
  57. * server.
  58. * @cfg {String} record The DomQuery path to the repeated element which contains
  59. * record information.
  60. * @cfg {String} success The DomQuery path to the success attribute used by
  61. * forms.
  62. * @cfg {String} id The DomQuery path relative from the record element to the
  63. * element that contains a record identifier value.
  64. * @constructor Create a new XmlReader.
  65. * @param {Object}
  66. * meta Metadata configuration options
  67. * @param {Object}
  68. * recordType Either an Array of field definition objects as passed
  69. * to {@link Ext.data.Record#create}, or a Record constructor object
  70. * created using {@link Ext.data.Record#create}.
  71. */
  72. Ext.data.XmlReader = function(meta, recordType) {
  73. meta = meta || {};
  74. Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType
  75. || meta.fields);
  76. };
  77. Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
  78. /**
  79. * This method is only used by a DataProxy which has retrieved data
  80. * from a remote server.
  81. *
  82. * @param {Object}
  83. * response The XHR object which contains the parsed XML
  84. * document. The response is expected to contain a
  85. * property called <tt>responseXML</tt> which refers to
  86. * an XML document object.
  87. * @return {Object} records A data block which is used by an
  88. * {@link Ext.data.Store} as a cache of Ext.data.Records.
  89. */
  90. read : function(response) {
  91. var doc = response.responseXML;
  92. if (!doc) {
  93. throw {
  94. message : "XmlReader.read: XML Document not available"
  95. };
  96. }
  97. return this.readRecords(doc);
  98. },
  99. /**
  100. * Create a data block containing Ext.data.Records from an XML
  101. * document.
  102. *
  103. * @param {Object}
  104. * doc A parsed XML document.
  105. * @return {Object} records A data block which is used by an
  106. * {@link Ext.data.Store} as a cache of Ext.data.Records.
  107. */
  108. readRecords : function(doc) {
  109. /**
  110. * After any data loads/reads, the raw XML Document is available
  111. * for further custom processing.
  112. *
  113. * @type XMLDocument
  114. */
  115. this.xmlData = doc;
  116. var root = doc.documentElement || doc;
  117. var q = Ext.DomQuery;
  118. var recordType = this.recordType, fields = recordType.prototype.fields;
  119. var sid = this.meta.id;
  120. var totalRecords = 0, success = true;
  121. if (this.meta.totalRecords) {
  122. totalRecords = q.selectNumber(this.meta.totalRecords, root,
  123. 0);
  124. }
  125. if (this.meta.success) {
  126. var sv = q.selectValue(this.meta.success, root, true);
  127. success = sv !== false && sv !== 'false';
  128. }
  129. var records = [];
  130. var ns = q.select(this.meta.record, root);
  131. for (var i = 0, len = ns.length; i < len; i++) {
  132. var n = ns[i];
  133. var values = {};
  134. var id = sid ? q.selectValue(sid, n) : undefined;
  135. for (var j = 0, jlen = fields.length; j < jlen; j++) {
  136. var f = fields.items[j];
  137. var v = q.selectValue(f.mapping || f.name, n,
  138. f.defaultValue);
  139. v = f.convert(v);
  140. values[f.name] = v;
  141. }
  142. var record = new recordType(values, id);
  143. record.node = n;
  144. records[records.length] = record;
  145. }
  146. return {
  147. success : success,
  148. records : records,
  149. totalRecords : totalRecords || records.length
  150. };
  151. }
  152. });