3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.data.XmlReader"></div>/**
15 * @class Ext.data.XmlReader
16 * @extends Ext.data.DataReader
17 * <p>Data reader class to create an Array of {@link Ext.data.Record} objects from an XML document
18 * based on mappings in a provided {@link Ext.data.Record} constructor.</p>
19 * <p><b>Note</b>: that in order for the browser to parse a returned XML document, the Content-Type
20 * header in the HTTP response must be set to "text/xml" or "application/xml".</p>
21 * <p>Example code:</p>
23 var Employee = Ext.data.Record.create([
24 {name: 'name', mapping: 'name'}, // "mapping" property not needed if it is the same as "name"
25 {name: 'occupation'} // This field will use "occupation" as the mapping.
27 var myReader = new Ext.data.XmlReader({
28 totalProperty: "results", // The element which contains the total dataset size (optional)
29 record: "row", // The repeated element which contains row information
30 idProperty: "id" // The element within the row that provides an ID for the record (optional)
31 messageProperty: "msg" // The element within the response that provides a user-feedback message (optional)
35 * This would consume an XML file like this:
37 <?xml version="1.0" encoding="UTF-8"?>
39 <results>2</results>
42 <name>Bill</name>
43 <occupation>Gardener</occupation>
47 <name>Ben</name>
48 <occupation>Horticulturalist</occupation>
52 * @cfg {String} totalProperty The DomQuery path from which to retrieve the total number of records
53 * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
54 * paged from the remote server.
55 * @cfg {String} record The DomQuery path to the repeated element which contains record information.
56 * @cfg {String} record The DomQuery path to the repeated element which contains record information.
57 * @cfg {String} successProperty The DomQuery path to the success attribute used by forms.
58 * @cfg {String} idPath The DomQuery path relative from the record element to the element that contains
59 * a record identifier value.
61 * Create a new XmlReader.
62 * @param {Object} meta Metadata configuration options
63 * @param {Object} recordType Either an Array of field definition objects as passed to
64 * {@link Ext.data.Record#create}, or a Record constructor object created using {@link Ext.data.Record#create}.
66 Ext.data.XmlReader = function(meta, recordType){
69 // backwards compat, convert idPath to idProperty
70 meta.idProperty = meta.idProperty || meta.idPath;
72 Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
74 Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
76 * This method is only used by a DataProxy which has retrieved data from a remote server.
77 * @param {Object} response The XHR object which contains the parsed XML document. The response is expected
78 * to contain a property called <tt>responseXML</tt> which refers to an XML document object.
79 * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
80 * a cache of Ext.data.Records.
82 read : function(response){
83 var doc = response.responseXML;
85 throw {message: "XmlReader.read: XML Document not available"};
87 return this.readRecords(doc);
91 * Create a data block containing Ext.data.Records from an XML document.
92 * @param {Object} doc A parsed XML document.
93 * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
94 * a cache of Ext.data.Records.
96 readRecords : function(doc){
98 * After any data loads/reads, the raw XML Document is available for further custom processing.
103 var root = doc.documentElement || doc,
108 if(this.meta.totalProperty){
109 totalRecords = this.getTotal(root, 0);
111 if(this.meta.successProperty){
112 success = this.getSuccess(root);
115 var records = this.extractData(q.select(this.meta.record, root), true); // <-- true to return Ext.data.Record[]
117 // TODO return Ext.data.Response instance. @see #readResponse
121 totalRecords : totalRecords || records.length
126 * Decode a json response from server.
127 * @param {String} action [{@link Ext.data.Api#actions} create|read|update|destroy]
128 * @param {Ext.data.Response} response Returns an instance of {@link Ext.data.Response}
130 readResponse : function(action, response) {
131 var q = Ext.DomQuery,
132 doc = response.responseXML;
134 var res = new Ext.data.Response({
136 success : this.getSuccess(doc),
137 message: this.getMessage(doc),
138 data: this.extractData(q.select(this.meta.record, doc) || q.select(this.meta.root, doc)),
142 if (Ext.isEmpty(res.success)) {
143 throw new Ext.data.DataReader.Error('successProperty-response', this.meta.successProperty);
146 if (action === Ext.data.Api.actions.create) {
147 var def = Ext.isDefined(res.data);
148 if (def && Ext.isEmpty(res.data)) {
149 throw new Ext.data.JsonReader.Error('root-empty', this.meta.root);
152 throw new Ext.data.JsonReader.Error('root-undefined-response', this.meta.root);
158 getSuccess : function() {
163 * build response-data extractor functions.
167 buildExtractors : function() {
172 Record = this.recordType,
173 f = Record.prototype.fields,
177 if(s.totalProperty) {
178 this.getTotal = this.createAccessor(s.totalProperty);
180 if(s.successProperty) {
181 this.getSuccess = this.createAccessor(s.successProperty);
183 if (s.messageProperty) {
184 this.getMessage = this.createAccessor(s.messageProperty);
186 this.getRoot = function(res) {
187 return (!Ext.isEmpty(res[this.meta.record])) ? res[this.meta.record] : res[this.meta.root];
189 if (s.idPath || s.idProperty) {
190 var g = this.createAccessor(s.idPath || s.idProperty);
191 this.getId = function(rec) {
192 var id = g(rec) || rec.id;
193 return (id === undefined || id === '') ? null : id;
196 this.getId = function(){return null;};
199 for(var i = 0; i < fl; i++){
201 var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
202 ef.push(this.createAccessor(map));
208 * Creates a function to return some particular key of data from a response.
209 * @param {String} key
214 createAccessor : function(){
215 var q = Ext.DomQuery;
216 return function(key) {
218 case this.meta.totalProperty:
219 return function(root, def){
220 return q.selectNumber(key, root, def);
223 case this.meta.successProperty:
224 return function(root, def) {
225 var sv = q.selectValue(key, root, true);
226 var success = sv !== false && sv !== 'false';
231 return function(root, def) {
232 return q.selectValue(key, root, def);
240 * Extracts rows of record-data from server. iterates and calls #extractValues
241 * TODO I don't care much for method-names of #extractData, #extractValues.
242 * @param {Array} root
243 * @param {Boolean} returnRecords When true, will return instances of Ext.data.Record; otherwise just hashes.
247 extractData : function(root, returnRecords) {
248 var Record = this.recordType,
250 f = Record.prototype.fields,
253 if (returnRecords === true) {
254 for (var i = 0, len = root.length; i < len; i++) {
256 record = new Record(this.extractValues(data, fi, fl), this.getId(data));
259 records.push(record);
262 for (var i = 0, len = root.length; i < len; i++) {
263 records.push(this.extractValues(root[i], fi, fl));
270 * extracts values and type-casts a row of data from server, extracted by #extractData
272 * @param {Ext.data.Field[]} items
273 * @param {Number} len
277 extractValues : function(data, items, len) {
279 for(var j = 0; j < len; j++){
281 var v = this.ef[j](data);
282 values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, data);