initial import
[roojs1] / Roo / data / XmlReader.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11
12 /**
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>
17  * <p>
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>
20  * <p>
21  * Example code:
22  * <pre><code>
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.
26 ]);
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)
31 }, RecordDef);
32 </code></pre>
33  * <p>
34  * This would consume an XML file like this:
35  * <pre><code>
36 &lt;?xml?>
37 &lt;dataset>
38  &lt;results>2&lt;/results>
39  &lt;row>
40    &lt;id>1&lt;/id>
41    &lt;name>Bill&lt;/name>
42    &lt;occupation>Gardener&lt;/occupation>
43  &lt;/row>
44  &lt;row>
45    &lt;id>2&lt;/id>
46    &lt;name>Ben&lt;/name>
47    &lt;occupation>Horticulturalist&lt;/occupation>
48  &lt;/row>
49 &lt;/dataset>
50 </code></pre>
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.
58  * @constructor
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.
64  */
65 Roo.data.XmlReader = function(meta, recordType){
66     meta = meta || {};
67     Roo.data.XmlReader.superclass.constructor.call(this, meta, recordType||meta.fields);
68 };
69 Roo.extend(Roo.data.XmlReader, Roo.data.DataReader, {
70     /**
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.
76      */
77     read : function(response){
78         var doc = response.responseXML;
79         if(!doc) {
80             throw {message: "XmlReader.read: XML Document not available"};
81         }
82         return this.readRecords(doc);
83     },
84
85     /**
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.
90      */
91     readRecords : function(doc){
92         /**
93          * After any data loads/reads, the raw XML Document is available for further custom processing.
94          * @type XMLDocument
95          */
96         this.xmlData = doc;
97         var root = doc.documentElement || doc;
98         var q = Roo.DomQuery;
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);
104         }
105         
106         if(this.meta.success){
107             var sv = q.selectValue(this.meta.success, root, true);
108             success = sv !== false && sv !== 'false';
109         }
110         var records = [];
111         var ns = q.select(this.meta.record, root);
112         for(var i = 0, len = ns.length; i < len; i++) {
113                 var n = ns[i];
114                 var values = {};
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);
119                     v = f.convert(v);
120                     values[f.name] = v;
121                 }
122                 var record = new recordType(values, id);
123                 record.node = n;
124                 records[records.length] = record;
125             }
126
127             return {
128                 success : success,
129                 records : records,
130                 totalRecords : totalRecords || records.length
131             };
132     }
133 });