2
* Ext JS Library 3.0 RC2
3
* Copyright(c) 2006-2009, Ext JS, LLC.
6
* http://extjs.com/license
10
* @class Ext.data.XmlReader
11
* @extends Ext.data.DataReader
12
* <p>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.</p>
14
* <p><b>Note</b>: that in order for the browser to parse a returned XML document, the Content-Type
15
* header in the HTTP response must be set to "text/xml" or "application/xml".</p>
16
* <p>Example code:</p>
18
var Employee = Ext.data.Record.create([
19
{name: 'name', mapping: 'name'}, // "mapping" property not needed if it is the same as "name"
20
{name: 'occupation'} // This field will use "occupation" as the mapping.
22
var myReader = new Ext.data.XmlReader({
23
totalRecords: "results", // The element which contains the total dataset size (optional)
24
record: "row", // The repeated element which contains row information
25
id: "id" // The element within the row that provides an ID for the record (optional)
29
* This would consume an XML file like this:
31
<?xml version="1.0" encoding="UTF-8"?>
33
<results>2</results>
36
<name>Bill</name>
37
<occupation>Gardener</occupation>
41
<name>Ben</name>
42
<occupation>Horticulturalist</occupation>
46
* @cfg {String} totalRecords The DomQuery path from which to retrieve the total number of records
47
* in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
48
* paged from the remote server.
49
* @cfg {String} record The DomQuery path to the repeated element which contains record information.
50
* @cfg {String} success The DomQuery path to the success attribute used by forms.
51
* @cfg {String} idPath The DomQuery path relative from the record element to the element that contains
52
* a record identifier value.
54
* Create a new XmlReader.
55
* @param {Object} meta Metadata configuration options
56
* @param {Object} recordType Either an Array of field definition objects as passed to
57
* {@link Ext.data.Record#create}, or a Record constructor object created using {@link Ext.data.Record#create}.
59
Ext.data.XmlReader = function(meta, recordType){
61
Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
63
Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
65
* This method is only used by a DataProxy which has retrieved data from a remote server.
66
* @param {Object} response The XHR object which contains the parsed XML document. The response is expected
67
* to contain a property called <tt>responseXML</tt> which refers to an XML document object.
68
* @return {Object} records A data block which is used by an {@link Ext.data.Store} as
69
* a cache of Ext.data.Records.
71
read : function(response){
72
var doc = response.responseXML;
74
throw {message: "XmlReader.read: XML Document not available"};
76
return this.readRecords(doc);
80
* Create a data block containing Ext.data.Records from an XML document.
81
* @param {Object} doc A parsed XML document.
82
* @return {Object} records A data block which is used by an {@link Ext.data.Store} as
83
* a cache of Ext.data.Records.
85
readRecords : function(doc){
87
* After any data loads/reads, the raw XML Document is available for further custom processing.
91
var root = doc.documentElement || doc;
93
var recordType = this.recordType, fields = recordType.prototype.fields;
94
var sid = this.meta.idPath || this.meta.id;
95
var totalRecords = 0, success = true;
96
if(this.meta.totalRecords){
97
totalRecords = q.selectNumber(this.meta.totalRecords, root, 0);
100
if(this.meta.success){
101
var sv = q.selectValue(this.meta.success, root, true);
102
success = sv !== false && sv !== 'false';
105
var ns = q.select(this.meta.record, root);
106
for(var i = 0, len = ns.length; i < len; i++) {
109
var id = sid ? q.selectValue(sid, n) : undefined;
110
for(var j = 0, jlen = fields.length; j < jlen; j++){
111
var f = fields.items[j];
112
var v = q.selectValue(Ext.value(f.mapping, f.name, true), n, f.defaultValue);
116
var record = new recordType(values, id);
118
records[records.length] = record;
124
totalRecords : totalRecords || records.length
128
// TODO: implement readResponse for XmlReader
129
readResponse : Ext.emptyFn
b'\\ No newline at end of file'