4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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 = Ext.create('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 (required)
190 * The DomQuery path to the repeated element which contains record information.
193 <span id='Ext-data-reader-Xml-method-createAccessor'> /**
195 * Creates a function to return some particular key of data from a response. The totalProperty and
196 * successProperty are treated as special cases for type casting, everything else is just a simple selector.
197 * @param {String} key
200 createAccessor: function(expr) {
203 if (Ext.isEmpty(expr)) {
207 if (Ext.isFunction(expr)) {
211 return function(root) {
212 return me.getNodeValue(Ext.DomQuery.selectNode(expr, root));
216 getNodeValue: function(node) {
217 if (node && node.firstChild) {
218 return node.firstChild.nodeValue;
224 getResponseData: function(response) {
225 var xml = response.responseXML;
231 msg: 'XML data not found in the response'
239 <span id='Ext-data-reader-Xml-method-getData'> /**
240 </span> * Normalizes the data object
241 * @param {Object} data The raw data object
242 * @return {Object} Returns the documentElement property of the data object if present, or the same object if not
244 getData: function(data) {
245 return data.documentElement || data;
248 <span id='Ext-data-reader-Xml-method-getRoot'> /**
250 * Given an XML object, returns the Element that represents the root as configured by the Reader's meta data
251 * @param {Object} data The XML data object
252 * @return {XMLElement} The root node element
254 getRoot: function(data) {
255 var nodeName = data.nodeName,
258 if (!root || (nodeName && nodeName == root)) {
260 } else if (Ext.DomQuery.isXml(data)) {
261 // This fix ensures we have XML data
262 // Related to TreeStore calling getRoot with the root node, which isn't XML
263 // Probably should be resolved in TreeStore at some point
264 return Ext.DomQuery.selectNode(root, data);
268 <span id='Ext-data-reader-Xml-method-extractData'> /**
270 * We're just preparing the data for the superclass by pulling out the record nodes we want
271 * @param {XMLElement} root The XML root node
272 * @return {Ext.data.Model[]} The records
274 extractData: function(root) {
275 var recordName = this.record;
279 Ext.Error.raise('Record is a required parameter');
283 if (recordName != root.nodeName) {
284 root = Ext.DomQuery.select(recordName, root);
288 return this.callParent([root]);
291 <span id='Ext-data-reader-Xml-method-getAssociatedDataRoot'> /**
293 * See Ext.data.reader.Reader's getAssociatedDataRoot docs
294 * @param {Object} data The raw data object
295 * @param {String} associationName The name of the association to get data for (uses associationKey if present)
296 * @return {XMLElement} The root
298 getAssociatedDataRoot: function(data, associationName) {
299 return Ext.DomQuery.select(associationName, data)[0];
302 <span id='Ext-data-reader-Xml-method-readRecords'> /**
303 </span> * Parses an XML document and returns a ResultSet containing the model instances
304 * @param {Object} doc Parsed XML document
305 * @return {Ext.data.ResultSet} The parsed result set
307 readRecords: function(doc) {
308 //it's possible that we get passed an array here by associations. Make sure we strip that out (see Ext.data.reader.Reader#readAssociated)
309 if (Ext.isArray(doc)) {
313 <span id='Ext-data-reader-Xml-property-xmlData'> /**
314 </span> * @deprecated will be removed in Ext JS 5.0. This is just a copy of this.rawData - use that instead
319 return this.callParent([doc]);