4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.data.XmlReader
14 * @extends Roo.data.DataReader
15 * Data reader class to create an Array of {@link Roo.data.Record} objects from an XML document
16 * based on mappings in a provided Roo.data.Record constructor.<br><br>
18 * <em>Note that in order for the browser to parse a returned XML document, the Content-Type
19 * header in the HTTP response must be set to "text/xml".</em>
23 var RecordDef = Roo.data.Record.create([
24 {name: 'name', mapping: 'name'}, // "mapping" property not needed if it's the same as "name"
25 {name: 'occupation'} // This field will use "occupation" as the mapping.
27 var myReader = new Roo.data.XmlReader({
28 totalRecords: "results", // The element which contains the total dataset size (optional)
29 record: "row", // The repeated element which contains row information
30 id: "id" // The element within the row that provides an ID for the record (optional)
34 * This would consume an XML file like this:
38 <results>2</results>
41 <name>Bill</name>
42 <occupation>Gardener</occupation>
46 <name>Ben</name>
47 <occupation>Horticulturalist</occupation>
51 * @cfg {String} totalRecords The DomQuery path from which to retrieve the total number of records
52 * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
53 * paged from the remote server.
54 * @cfg {String} record The DomQuery path to the repeated element which contains record information.
55 * @cfg {String} success The DomQuery path to the success attribute used by forms.
56 * @cfg {String} id The DomQuery path relative from the record element to the element that contains
57 * a record identifier value.
59 * Create a new XmlReader
60 * @param {Object} meta Metadata configuration options
61 * @param {Mixed} recordType The definition of the data record type to produce. This can be either a valid
62 * Record subclass created with {@link Roo.data.Record#create}, or an array of objects with which to call
63 * Roo.data.Record.create. See the {@link Roo.data.Record} class for more details.
65 Roo.data.XmlReader = function(meta, recordType){
67 Roo.data.XmlReader.superclass.constructor.call(this, meta, recordType||meta.fields);
69 Roo.extend(Roo.data.XmlReader, Roo.data.DataReader, {
71 * This method is only used by a DataProxy which has retrieved data from a remote server.
72 * @param {Object} response The XHR object which contains the parsed XML document. The response is expected
73 * to contain a method called 'responseXML' that returns an XML document object.
74 * @return {Object} records A data block which is used by an {@link Roo.data.Store} as
75 * a cache of Roo.data.Records.
77 read : function(response){
78 var doc = response.responseXML;
80 throw {message: "XmlReader.read: XML Document not available"};
82 return this.readRecords(doc);
86 * Create a data block containing Roo.data.Records from an XML document.
87 * @param {Object} doc A parsed XML document.
88 * @return {Object} records A data block which is used by an {@link Roo.data.Store} as
89 * a cache of Roo.data.Records.
91 readRecords : function(doc){
93 * After any data loads/reads, the raw XML Document is available for further custom processing.
97 var root = doc.documentElement || doc;
99 var recordType = this.recordType, fields = recordType.prototype.fields;
100 var sid = this.meta.id;
101 var totalRecords = 0, success = true;
102 if(this.meta.totalRecords){
103 totalRecords = q.selectNumber(this.meta.totalRecords, root, 0);
106 if(this.meta.success){
107 var sv = q.selectValue(this.meta.success, root, true);
108 success = sv !== false && sv !== 'false';
111 var ns = q.select(this.meta.record, root);
112 for(var i = 0, len = ns.length; i < len; i++) {
115 var id = sid ? q.selectValue(sid, n) : undefined;
116 for(var j = 0, jlen = fields.length; j < jlen; j++){
117 var f = fields.items[j];
118 var v = q.selectValue(f.mapping || f.name, n, f.defaultValue);
122 var record = new recordType(values, id);
124 records[records.length] = record;
130 totalRecords : totalRecords || records.length