4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-data-reader-Xml'>/**
19 </span> * @author Ed Spencer
20 * @class Ext.data.reader.Xml
21 * @extends Ext.data.reader.Reader
23 * <p>The XML Reader is used by a Proxy to read a server response that is sent back in XML format. This usually
24 * happens as a result of loading a Store - for example we might create something like this:</p>
26 <pre><code>
28 extend: 'Ext.data.Model',
29 fields: ['id', 'name', 'email']
32 var store = new Ext.data.Store({
43 </code></pre>
45 * <p>The example above creates a 'User' model. Models are explained in the {@link Ext.data.Model Model} docs if you're
46 * not already familiar with them.</p>
48 * <p>We created the simplest type of XML Reader possible by simply telling our {@link Ext.data.Store Store}'s
49 * {@link Ext.data.proxy.Proxy Proxy} that we want a XML Reader. The Store automatically passes the configured model to the
50 * Store, so it is as if we passed this instead:
52 <pre><code>
58 </code></pre>
60 * <p>The reader we set up is ready to read data from our server - at the moment it will accept a response like this:</p>
62 <pre><code>
63 &lt;?xml version="1.0" encoding="UTF-8"?&gt;
65 &lt;id&gt;1&lt;/id&gt;
66 &lt;name&gt;Ed Spencer&lt;/name&gt;
67 &lt;email&gt;ed@sencha.com&lt;/email&gt;
70 &lt;id&gt;2&lt;/id&gt;
71 &lt;name&gt;Abe Elias&lt;/name&gt;
72 &lt;email&gt;abe@sencha.com&lt;/email&gt;
74 </code></pre>
76 * <p>The XML Reader uses the configured {@link #record} option to pull out the data for each record - in this case we
77 * set record to 'user', so each &lt;user&gt; above will be converted into a User model.</p>
79 * <p><u>Reading other XML formats</u></p>
81 * <p>If you already have your XML format defined and it doesn't look quite like what we have above, you can usually
82 * pass XmlReader a couple of configuration options to make it parse your format. For example, we can use the
83 * {@link #root} configuration to parse data that comes back like this:</p>
85 <pre><code>
86 &lt;?xml version="1.0" encoding="UTF-8"?&gt;
89 &lt;id&gt;1&lt;/id&gt;
90 &lt;name&gt;Ed Spencer&lt;/name&gt;
91 &lt;email&gt;ed@sencha.com&lt;/email&gt;
94 &lt;id&gt;2&lt;/id&gt;
95 &lt;name&gt;Abe Elias&lt;/name&gt;
96 &lt;email&gt;abe@sencha.com&lt;/email&gt;
98 &lt;/users&gt;
99 </code></pre>
101 * <p>To parse this we just pass in a {@link #root} configuration that matches the 'users' above:</p>
103 <pre><code>
109 </code></pre>
111 * <p>Note that XmlReader doesn't care whether your {@link #root} and {@link #record} elements are nested deep inside
112 * a larger structure, so a response like this will still work:
114 <pre><code>
115 &lt;?xml version="1.0" encoding="UTF-8"?&gt;
116 &lt;deeply&gt;
117 &lt;nested&gt;
119 &lt;users&gt;
121 &lt;id&gt;1&lt;/id&gt;
122 &lt;name&gt;Ed Spencer&lt;/name&gt;
123 &lt;email&gt;ed@sencha.com&lt;/email&gt;
124 &lt;/user&gt;
126 &lt;id&gt;2&lt;/id&gt;
127 &lt;name&gt;Abe Elias&lt;/name&gt;
128 &lt;email&gt;abe@sencha.com&lt;/email&gt;
129 &lt;/user&gt;
130 &lt;/users&gt;
132 &lt;/nested&gt;
133 &lt;/deeply&gt;
134 </code></pre>
136 * <p><u>Response metadata</u></p>
138 * <p>The server can return additional data in its response, such as the {@link #totalProperty total number of records}
139 * and the {@link #successProperty success status of the response}. These are typically included in the XML response
140 * like this:</p>
142 <pre><code>
143 &lt;?xml version="1.0" encoding="UTF-8"?&gt;
144 &lt;total&gt;100&lt;/total&gt;
145 &lt;success&gt;true&lt;/success&gt;
146 &lt;users&gt;
148 &lt;id&gt;1&lt;/id&gt;
149 &lt;name&gt;Ed Spencer&lt;/name&gt;
150 &lt;email&gt;ed@sencha.com&lt;/email&gt;
151 &lt;/user&gt;
153 &lt;id&gt;2&lt;/id&gt;
154 &lt;name&gt;Abe Elias&lt;/name&gt;
155 &lt;email&gt;abe@sencha.com&lt;/email&gt;
156 &lt;/user&gt;
157 &lt;/users&gt;
158 </code></pre>
160 * <p>If these properties are present in the XML response they can be parsed out by the XmlReader and used by the
161 * Store that loaded it. We can set up the names of these properties by specifying a final pair of configuration
164 <pre><code>
168 totalProperty : 'total',
169 successProperty: 'success'
171 </code></pre>
173 * <p>These final options are not necessary to make the Reader work, but can be useful when the server needs to report
174 * an error or if it needs to indicate that there is a lot of data available of which only a subset is currently being
175 * returned.</p>
177 * <p><u>Response format</u></p>
179 * <p><b>Note:</b> in order for the browser to parse a returned XML document, the Content-Type header in the HTTP
180 * response must be set to "text/xml" or "application/xml". This is very important - the XmlReader will not
181 * work correctly otherwise.</p>
183 Ext.define('Ext.data.reader.Xml', {
184 extend: 'Ext.data.reader.Reader',
185 alternateClassName: 'Ext.data.XmlReader',
186 alias : 'reader.xml',
188 <span id='Ext-data-reader-Xml-cfg-record'> /**
189 </span> * @cfg {String} record The DomQuery path to the repeated element which contains record information.
192 <span id='Ext-data-reader-Xml-method-createAccessor'> /**
194 * Creates a function to return some particular key of data from a response. The totalProperty and
195 * successProperty are treated as special cases for type casting, everything else is just a simple selector.
196 * @param {String} key
199 createAccessor: function(expr) {
202 if (Ext.isEmpty(expr)) {
206 if (Ext.isFunction(expr)) {
210 return function(root) {
211 var node = Ext.DomQuery.selectNode(expr, root),
212 val = me.getNodeValue(node);
214 return Ext.isEmpty(val) ? null : val;
218 getNodeValue: function(node) {
220 if (node && node.firstChild) {
221 val = node.firstChild.nodeValue;
227 getResponseData: function(response) {
228 var xml = response.responseXML;
234 msg: 'XML data not found in the response'
242 <span id='Ext-data-reader-Xml-method-getData'> /**
243 </span> * Normalizes the data object
244 * @param {Object} data The raw data object
245 * @return {Object} Returns the documentElement property of the data object if present, or the same object if not
247 getData: function(data) {
248 return data.documentElement || data;
251 <span id='Ext-data-reader-Xml-method-getRoot'> /**
253 * Given an XML object, returns the Element that represents the root as configured by the Reader's meta data
254 * @param {Object} data The XML data object
255 * @return {Element} The root node element
257 getRoot: function(data) {
258 var nodeName = data.nodeName,
261 if (!root || (nodeName && nodeName == root)) {
263 } else if (Ext.DomQuery.isXml(data)) {
264 // This fix ensures we have XML data
265 // Related to TreeStore calling getRoot with the root node, which isn't XML
266 // Probably should be resolved in TreeStore at some point
267 return Ext.DomQuery.selectNode(root, data);
271 <span id='Ext-data-reader-Xml-method-extractData'> /**
273 * We're just preparing the data for the superclass by pulling out the record nodes we want
274 * @param {Element} root The XML root node
275 * @return {Array} The records
277 extractData: function(root) {
278 var recordName = this.record;
282 Ext.Error.raise('Record is a required parameter');
286 if (recordName != root.nodeName) {
287 root = Ext.DomQuery.select(recordName, root);
291 return this.callParent([root]);
294 <span id='Ext-data-reader-Xml-method-getAssociatedDataRoot'> /**
296 * See Ext.data.reader.Reader's getAssociatedDataRoot docs
297 * @param {Mixed} data The raw data object
298 * @param {String} associationName The name of the association to get data for (uses associationKey if present)
299 * @return {Mixed} The root
301 getAssociatedDataRoot: function(data, associationName) {
302 return Ext.DomQuery.select(associationName, data)[0];
305 <span id='Ext-data-reader-Xml-method-readRecords'> /**
306 </span> * Parses an XML document and returns a ResultSet containing the model instances
307 * @param {Object} doc Parsed XML document
308 * @return {Ext.data.ResultSet} The parsed result set
310 readRecords: function(doc) {
311 //it's possible that we get passed an array here by associations. Make sure we strip that out (see Ext.data.reader.Reader#readAssociated)
312 if (Ext.isArray(doc)) {
316 <span id='Ext-data-reader-Xml-property-xmlData'> /**
317 </span> * DEPRECATED - will be removed in Ext JS 5.0. This is just a copy of this.rawData - use that instead
322 return this.callParent([doc]);