1
<html><head><title>XmlReader.js</title><link rel="stylesheet" type="text/css" href="../resources/style.css" media="screen"/></head><body><h1>XmlReader.js</h1><pre class="highlighted"><code><i>/**
2
* @class Ext.data.XmlReader
3
* @extends Ext.data.DataReader
4
* Data reader class to create an Array of {@link Ext.data.Record} objects from an XML document
5
* based on mappings <b>in</b> a provided {@link Ext.data.Record} constructor.<br><br>
7
* <em>Note that <b>in</b> order <b>for</b> the browser to parse a returned XML document, the Content-Type
8
* header <b>in</b> the HTTP response must be set to "text/xml" or "application/xml".</em>
11
* <pre><code>
12
<b>var</b> Employee = Ext.data.Record.create([
13
{name: <em>'name'</em>, mapping: <em>'name'</em>}, <i>// "mapping" property not needed <b>if</b> it's the same as "name"</i>
14
{name: <em>'occupation'</em>} <i>// This field will use "occupation" as the mapping.</i>
16
<b>var</b> myReader = <b>new</b> Ext.data.XmlReader({
17
totalRecords: "results", <i>// The element which contains the total dataset size (optional)</i>
18
record: "row", <i>// The repeated element which contains row information</i>
19
id: "id" <i>// The element within the row that provides an ID <b>for</b> the record (optional)</i>
21
</code></pre>
23
* This would consume an XML file like <b>this</b>:
24
* <pre><code>
25
&lt;?xml version="1.0" encoding="UTF-8"?>
27
&lt;results>2&lt;/results>
29
&lt;id>1&lt;/id>
30
&lt;name>Bill&lt;/name>
31
&lt;occupation>Gardener&lt;/occupation>
34
&lt;id>2&lt;/id>
35
&lt;name>Ben&lt;/name>
36
&lt;occupation>Horticulturalist&lt;/occupation>
39
</code></pre>
40
* @cfg {String} totalRecords The DomQuery path from which to retrieve the total number of records
41
* <b>in</b> the dataset. This is only needed <b>if</b> the whole dataset is not passed <b>in</b> one go, but is being
42
* paged from the remote server.
43
* @cfg {String} record The DomQuery path to the repeated element which contains record information.
44
* @cfg {String} success The DomQuery path to the success attribute used by forms.
45
* @cfg {String} id The DomQuery path relative from the record element to the element that contains
46
* a record identifier value.
48
* Create a <b>new</b> XmlReader.
49
* @param {Object} meta Metadata configuration options
50
* @param {Object} recordType Either an Array of field definition objects as passed to
51
* {@link Ext.data.Record#create}, or a Record constructor object created using {@link Ext.data.Record#create}.
53
Ext.data.XmlReader = <b>function</b>(meta, recordType){
55
Ext.data.XmlReader.superclass.constructor.call(<b>this</b>, meta, recordType || meta.fields);
57
Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
59
* This method is only used by a DataProxy which has retrieved data from a remote server.
60
* @param {Object} response The XHR object which contains the parsed XML document. The response is expected
61
* to contain a property called <tt>responseXML</tt> which refers to an XML document object.
62
* @<b>return</b> {Object} records A data block which is used by an {@link Ext.data.Store} as
63
* a cache of Ext.data.Records.
65
read : <b>function</b>(response){
66
<b>var</b> doc = response.responseXML;
68
throw {message: "XmlReader.read: XML Document not available"};
70
<b>return</b> this.readRecords(doc);
74
* Create a data block containing Ext.data.Records from an XML document.
75
* @param {Object} doc A parsed XML document.
76
* @<b>return</b> {Object} records A data block which is used by an {@link Ext.data.Store} as
77
* a cache of Ext.data.Records.
79
readRecords : <b>function</b>(doc){
81
* After any data loads/reads, the raw XML Document is available <b>for</b> further custom processing.
84
<b>this</b>.xmlData = doc;
85
<b>var</b> root = doc.documentElement || doc;
86
<b>var</b> q = Ext.DomQuery;
87
<b>var</b> recordType = <b>this</b>.recordType, fields = recordType.prototype.fields;
88
<b>var</b> sid = <b>this</b>.meta.id;
89
<b>var</b> totalRecords = 0, success = true;
90
<b>if</b>(this.meta.totalRecords){
91
totalRecords = q.selectNumber(<b>this</b>.meta.totalRecords, root, 0);
94
<b>if</b>(this.meta.success){
95
<b>var</b> sv = q.selectValue(<b>this</b>.meta.success, root, true);
96
success = sv !== false && sv !== <em>'false'</em>;
98
<b>var</b> records = [];
99
<b>var</b> ns = q.select(<b>this</b>.meta.record, root);
100
<b>for</b>(var i = 0, len = ns.length; i < len; i++) {
101
<b>var</b> n = ns[i];
102
<b>var</b> values = {};
103
<b>var</b> id = sid ? q.selectValue(sid, n) : undefined;
104
<b>for</b>(var j = 0, jlen = fields.length; j < jlen; j++){
105
<b>var</b> f = fields.items[j];
106
<b>var</b> v = q.selectValue(Ext.isEmpty(f.mapping, true) ? f.name : f.mapping, n, f.defaultValue);
110
<b>var</b> record = <b>new</b> recordType(values, id);
112
records[records.length] = record;
118
totalRecords : totalRecords || records.length
121
});</code></pre><hr><div style="font-size:10px;text-align:center;color:gray;">Ext - Copyright © 2006-2007 Ext JS, LLC<br />All rights reserved.</div>
b'\\ No newline at end of file'