3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.data.XmlReader"></div>/**
9 * @class Ext.data.XmlReader
10 * @extends Ext.data.DataReader
11 * <p>Data reader class to create an Array of {@link Ext.data.Record} objects from an XML document
12 * based on mappings in a provided {@link Ext.data.Record} constructor.</p>
13 * <p><b>Note</b>: that in order for the browser to parse a returned XML document, the Content-Type
14 * header in the HTTP response must be set to "text/xml" or "application/xml".</p>
15 * <p>Example code:</p>
17 var Employee = Ext.data.Record.create([
18 {name: 'name', mapping: 'name'}, // "mapping" property not needed if it is the same as "name"
19 {name: 'occupation'} // This field will use "occupation" as the mapping.
21 var myReader = new Ext.data.XmlReader({
22 totalRecords: "results", // The element which contains the total dataset size (optional)
23 record: "row", // The repeated element which contains row information
24 id: "id" // The element within the row that provides an ID for the record (optional)
28 * This would consume an XML file like this:
30 <?xml version="1.0" encoding="UTF-8"?>
32 <results>2</results>
35 <name>Bill</name>
36 <occupation>Gardener</occupation>
40 <name>Ben</name>
41 <occupation>Horticulturalist</occupation>
45 * @cfg {String} totalRecords The DomQuery path from which to retrieve the total number of records
46 * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
47 * paged from the remote server.
48 * @cfg {String} record The DomQuery path to the repeated element which contains record information.
49 * @cfg {String} success The DomQuery path to the success attribute used by forms.
50 * @cfg {String} idPath The DomQuery path relative from the record element to the element that contains
51 * a record identifier value.
53 * Create a new XmlReader.
54 * @param {Object} meta Metadata configuration options
55 * @param {Object} recordType Either an Array of field definition objects as passed to
56 * {@link Ext.data.Record#create}, or a Record constructor object created using {@link Ext.data.Record#create}.
58 Ext.data.XmlReader = function(meta, recordType){
60 Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
62 Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
64 * This method is only used by a DataProxy which has retrieved data from a remote server.
65 * @param {Object} response The XHR object which contains the parsed XML document. The response is expected
66 * to contain a property called <tt>responseXML</tt> which refers to an XML document object.
67 * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
68 * a cache of Ext.data.Records.
70 read : function(response){
71 var doc = response.responseXML;
73 throw {message: "XmlReader.read: XML Document not available"};
75 return this.readRecords(doc);
79 * Create a data block containing Ext.data.Records from an XML document.
80 * @param {Object} doc A parsed XML document.
81 * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
82 * a cache of Ext.data.Records.
84 readRecords : function(doc){
86 * After any data loads/reads, the raw XML Document is available for further custom processing.
90 var root = doc.documentElement || doc;
92 var recordType = this.recordType, fields = recordType.prototype.fields;
93 var sid = this.meta.idPath || this.meta.id;
94 var totalRecords = 0, success = true;
95 if(this.meta.totalRecords){
96 totalRecords = q.selectNumber(this.meta.totalRecords, root, 0);
99 if(this.meta.success){
100 var sv = q.selectValue(this.meta.success, root, true);
101 success = sv !== false && sv !== 'false';
104 var ns = q.select(this.meta.record, root);
105 for(var i = 0, len = ns.length; i < len; i++) {
108 var id = sid ? q.selectValue(sid, n) : undefined;
109 for(var j = 0, jlen = fields.length; j < jlen; j++){
110 var f = fields.items[j];
111 var v = q.selectValue(Ext.value(f.mapping, f.name, true), n, f.defaultValue);
115 var record = new recordType(values, id);
117 records[records.length] = record;
123 totalRecords : totalRecords || records.length
127 // TODO: implement readResponse for XmlReader
128 readResponse : Ext.emptyFn