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, {
74 * This method is only used by a DataProxy which has retrieved data from a remote server.
75 * @param {Object} response The XHR object which contains the parsed XML document. The response is expected
76 * to contain a method called 'responseXML' that returns an XML document object.
77 * @return {Object} records A data block which is used by an {@link Roo.data.Store} as
78 * a cache of Roo.data.Records.
80 read : function(response){
81 var doc = response.responseXML;
83 throw {message: "XmlReader.read: XML Document not available"};
85 return this.readRecords(doc);
89 * Create a data block containing Roo.data.Records from an XML document.
90 * @param {Object} doc A parsed XML document.
91 * @return {Object} records A data block which is used by an {@link Roo.data.Store} as
92 * a cache of Roo.data.Records.
94 readRecords : function(doc){
96 * After any data loads/reads, the raw XML Document is available for further custom processing.
100 var root = doc.documentElement || doc;
101 var q = Roo.DomQuery;
102 var recordType = this.recordType, fields = recordType.prototype.fields;
103 var sid = this.meta.id;
104 var totalRecords = 0, success = true;
105 if(this.meta.totalRecords){
106 totalRecords = q.selectNumber(this.meta.totalRecords, root, 0);
109 if(this.meta.success){
110 var sv = q.selectValue(this.meta.success, root, true);
111 success = sv !== false && sv !== 'false';
114 var ns = q.select(this.meta.record, root);
115 for(var i = 0, len = ns.length; i < len; i++) {
118 var id = sid ? q.selectValue(sid, n) : undefined;
119 for(var j = 0, jlen = fields.length; j < jlen; j++){
120 var f = fields.items[j];
121 var v = q.selectValue(f.mapping || f.name, n, f.defaultValue);
125 var record = new recordType(values, id);
127 records[records.length] = record;
133 totalRecords : totalRecords || records.length