3
* Copyright(c) 2006-2008, Ext JS, LLC.
6
* http://extjs.com/license
10
* @class Ext.data.XmlReader
11
* @extends Ext.data.DataReader
12
* Data reader class to create an Array of {@link Ext.data.Record} objects from an XML document
13
* based on mappings in a provided {@link Ext.data.Record} constructor.<br><br>
15
* <em>Note that in order for the browser to parse a returned XML document, the Content-Type
16
* header in the HTTP response must be set to "text/xml" or "application/xml".</em>
20
var Employee = Ext.data.Record.create([
21
{name: 'name', mapping: 'name'}, // "mapping" property not needed if it's the same as "name"
22
{name: 'occupation'} // This field will use "occupation" as the mapping.
24
var myReader = new Ext.data.XmlReader({
25
totalRecords: "results", // The element which contains the total dataset size (optional)
26
record: "row", // The repeated element which contains row information
27
id: "id" // The element within the row that provides an ID for the record (optional)
31
* This would consume an XML file like this:
33
<?xml version="1.0" encoding="UTF-8"?>
35
<results>2</results>
38
<name>Bill</name>
39
<occupation>Gardener</occupation>
43
<name>Ben</name>
44
<occupation>Horticulturalist</occupation>
48
* @cfg {String} totalRecords The DomQuery path from which to retrieve the total number of records
49
* in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
50
* paged from the remote server.
51
* @cfg {String} record The DomQuery path to the repeated element which contains record information.
52
* @cfg {String} success The DomQuery path to the success attribute used by forms.
53
* @cfg {String} id The DomQuery path relative from the record element to the element that contains
54
* a record identifier value.
56
* Create a new XmlReader.
57
* @param {Object} meta Metadata configuration options
58
* @param {Object} recordType Either an Array of field definition objects as passed to
59
* {@link Ext.data.Record#create}, or a Record constructor object created using {@link Ext.data.Record#create}.
61
Ext.data.XmlReader = function(meta, recordType){
63
Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
65
Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
67
* This method is only used by a DataProxy which has retrieved data from a remote server.
68
* @param {Object} response The XHR object which contains the parsed XML document. The response is expected
69
* to contain a property called <tt>responseXML</tt> which refers to an XML document object.
70
* @return {Object} records A data block which is used by an {@link Ext.data.Store} as
71
* a cache of Ext.data.Records.
73
read : function(response){
74
var doc = response.responseXML;
76
throw {message: "XmlReader.read: XML Document not available"};
78
return this.readRecords(doc);
82
* Create a data block containing Ext.data.Records from an XML document.
83
* @param {Object} doc A parsed XML document.
84
* @return {Object} records A data block which is used by an {@link Ext.data.Store} as
85
* a cache of Ext.data.Records.
87
readRecords : function(doc){
89
* After any data loads/reads, the raw XML Document is available for further custom processing.
93
var root = doc.documentElement || doc;
95
var recordType = this.recordType, fields = recordType.prototype.fields;
96
var sid = this.meta.id;
97
var totalRecords = 0, success = true;
98
if(this.meta.totalRecords){
99
totalRecords = q.selectNumber(this.meta.totalRecords, root, 0);
102
if(this.meta.success){
103
var sv = q.selectValue(this.meta.success, root, true);
104
success = sv !== false && sv !== 'false';
107
var ns = q.select(this.meta.record, root);
108
for(var i = 0, len = ns.length; i < len; i++) {
111
var id = sid ? q.selectValue(sid, n) : undefined;
112
for(var j = 0, jlen = fields.length; j < jlen; j++){
113
var f = fields.items[j];
114
var v = q.selectValue(f.mapping || f.name, n, f.defaultValue);
118
var record = new recordType(values, id);
120
records[records.length] = record;
126
totalRecords : totalRecords || records.length
b'\\ No newline at end of file'