3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.data.XmlReader
9 * @extends Ext.data.DataReader
10 * <p>Data reader class to create an Array of {@link Ext.data.Record} objects from an XML document
11 * based on mappings in a provided {@link Ext.data.Record} constructor.</p>
12 * <p><b>Note</b>: that in order for the browser to parse a returned XML document, the Content-Type
13 * header in the HTTP response must be set to "text/xml" or "application/xml".</p>
14 * <p>Example code:</p>
16 var Employee = Ext.data.Record.create([
17 {name: 'name', mapping: 'name'}, // "mapping" property not needed if it is the same as "name"
18 {name: 'occupation'} // This field will use "occupation" as the mapping.
20 var myReader = new Ext.data.XmlReader({
21 totalRecords: "results", // The element which contains the total dataset size (optional)
22 record: "row", // The repeated element which contains row information
23 id: "id" // The element within the row that provides an ID for the record (optional)
27 * This would consume an XML file like this:
29 <?xml version="1.0" encoding="UTF-8"?>
31 <results>2</results>
34 <name>Bill</name>
35 <occupation>Gardener</occupation>
39 <name>Ben</name>
40 <occupation>Horticulturalist</occupation>
44 * @cfg {String} totalRecords The DomQuery path from which to retrieve the total number of records
45 * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
46 * paged from the remote server.
47 * @cfg {String} record The DomQuery path to the repeated element which contains record information.
48 * @cfg {String} success The DomQuery path to the success attribute used by forms.
49 * @cfg {String} idPath The DomQuery path relative from the record element to the element that contains
50 * a record identifier value.
52 * Create a new XmlReader.
53 * @param {Object} meta Metadata configuration options
54 * @param {Object} recordType Either an Array of field definition objects as passed to
55 * {@link Ext.data.Record#create}, or a Record constructor object created using {@link Ext.data.Record#create}.
57 Ext.data.XmlReader = function(meta, recordType){
59 Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
61 Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
63 * This method is only used by a DataProxy which has retrieved data from a remote server.
64 * @param {Object} response The XHR object which contains the parsed XML document. The response is expected
65 * to contain a property called <tt>responseXML</tt> which refers to an XML document object.
66 * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
67 * a cache of Ext.data.Records.
69 read : function(response){
70 var doc = response.responseXML;
72 throw {message: "XmlReader.read: XML Document not available"};
74 return this.readRecords(doc);
78 * Create a data block containing Ext.data.Records from an XML document.
79 * @param {Object} doc A parsed XML document.
80 * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
81 * a cache of Ext.data.Records.
83 readRecords : function(doc){
85 * After any data loads/reads, the raw XML Document is available for further custom processing.
89 var root = doc.documentElement || doc;
91 var recordType = this.recordType, fields = recordType.prototype.fields;
92 var sid = this.meta.idPath || this.meta.id;
93 var totalRecords = 0, success = true;
94 if(this.meta.totalRecords){
95 totalRecords = q.selectNumber(this.meta.totalRecords, root, 0);
98 if(this.meta.success){
99 var sv = q.selectValue(this.meta.success, root, true);
100 success = sv !== false && sv !== 'false';
103 var ns = q.select(this.meta.record, root);
104 for(var i = 0, len = ns.length; i < len; i++) {
107 var id = sid ? q.selectValue(sid, n) : undefined;
108 for(var j = 0, jlen = fields.length; j < jlen; j++){
109 var f = fields.items[j];
110 var v = q.selectValue(Ext.value(f.mapping, f.name, true), n, f.defaultValue);
114 var record = new recordType(values, id);
116 records[records.length] = record;
122 totalRecords : totalRecords || records.length
126 // TODO: implement readResponse for XmlReader
127 readResponse : Ext.emptyFn