3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
17 * @class Ext.data.reader.Xml
18 * @extends Ext.data.reader.Reader
20 * <p>The XML Reader is used by a Proxy to read a server response that is sent back in XML format. This usually
21 * happens as a result of loading a Store - for example we might create something like this:</p>
25 extend: 'Ext.data.Model',
26 fields: ['id', 'name', 'email']
29 var store = Ext.create('Ext.data.Store', {
42 * <p>The example above creates a 'User' model. Models are explained in the {@link Ext.data.Model Model} docs if you're
43 * not already familiar with them.</p>
45 * <p>We created the simplest type of XML Reader possible by simply telling our {@link Ext.data.Store Store}'s
46 * {@link Ext.data.proxy.Proxy Proxy} that we want a XML Reader. The Store automatically passes the configured model to the
47 * Store, so it is as if we passed this instead:
57 * <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>
60 <?xml version="1.0" encoding="UTF-8"?>
62 <id>1</id>
63 <name>Ed Spencer</name>
64 <email>ed@sencha.com</email>
67 <id>2</id>
68 <name>Abe Elias</name>
69 <email>abe@sencha.com</email>
73 * <p>The XML Reader uses the configured {@link #record} option to pull out the data for each record - in this case we
74 * set record to 'user', so each <user> above will be converted into a User model.</p>
76 * <p><u>Reading other XML formats</u></p>
78 * <p>If you already have your XML format defined and it doesn't look quite like what we have above, you can usually
79 * pass XmlReader a couple of configuration options to make it parse your format. For example, we can use the
80 * {@link #root} configuration to parse data that comes back like this:</p>
83 <?xml version="1.0" encoding="UTF-8"?>
86 <id>1</id>
87 <name>Ed Spencer</name>
88 <email>ed@sencha.com</email>
91 <id>2</id>
92 <name>Abe Elias</name>
93 <email>abe@sencha.com</email>
98 * <p>To parse this we just pass in a {@link #root} configuration that matches the 'users' above:</p>
108 * <p>Note that XmlReader doesn't care whether your {@link #root} and {@link #record} elements are nested deep inside
109 * a larger structure, so a response like this will still work:
112 <?xml version="1.0" encoding="UTF-8"?>
118 <id>1</id>
119 <name>Ed Spencer</name>
120 <email>ed@sencha.com</email>
123 <id>2</id>
124 <name>Abe Elias</name>
125 <email>abe@sencha.com</email>
133 * <p><u>Response metadata</u></p>
135 * <p>The server can return additional data in its response, such as the {@link #totalProperty total number of records}
136 * and the {@link #successProperty success status of the response}. These are typically included in the XML response
140 <?xml version="1.0" encoding="UTF-8"?>
141 <total>100</total>
142 <success>true</success>
145 <id>1</id>
146 <name>Ed Spencer</name>
147 <email>ed@sencha.com</email>
150 <id>2</id>
151 <name>Abe Elias</name>
152 <email>abe@sencha.com</email>
157 * <p>If these properties are present in the XML response they can be parsed out by the XmlReader and used by the
158 * Store that loaded it. We can set up the names of these properties by specifying a final pair of configuration
165 totalProperty : 'total',
166 successProperty: 'success'
170 * <p>These final options are not necessary to make the Reader work, but can be useful when the server needs to report
171 * an error or if it needs to indicate that there is a lot of data available of which only a subset is currently being
174 * <p><u>Response format</u></p>
176 * <p><b>Note:</b> in order for the browser to parse a returned XML document, the Content-Type header in the HTTP
177 * response must be set to "text/xml" or "application/xml". This is very important - the XmlReader will not
178 * work correctly otherwise.</p>
180 Ext.define('Ext.data.reader.Xml', {
181 extend: 'Ext.data.reader.Reader',
182 alternateClassName: 'Ext.data.XmlReader',
183 alias : 'reader.xml',
186 * @cfg {String} record (required)
187 * The DomQuery path to the repeated element which contains record information.
192 * Creates a function to return some particular key of data from a response. The totalProperty and
193 * successProperty are treated as special cases for type casting, everything else is just a simple selector.
194 * @param {String} key
197 createAccessor: function(expr) {
200 if (Ext.isEmpty(expr)) {
204 if (Ext.isFunction(expr)) {
208 return function(root) {
209 return me.getNodeValue(Ext.DomQuery.selectNode(expr, root));
213 getNodeValue: function(node) {
214 if (node && node.firstChild) {
215 return node.firstChild.nodeValue;
221 getResponseData: function(response) {
222 var xml = response.responseXML;
228 msg: 'XML data not found in the response'
237 * Normalizes the data object
238 * @param {Object} data The raw data object
239 * @return {Object} Returns the documentElement property of the data object if present, or the same object if not
241 getData: function(data) {
242 return data.documentElement || data;
247 * Given an XML object, returns the Element that represents the root as configured by the Reader's meta data
248 * @param {Object} data The XML data object
249 * @return {XMLElement} The root node element
251 getRoot: function(data) {
252 var nodeName = data.nodeName,
255 if (!root || (nodeName && nodeName == root)) {
257 } else if (Ext.DomQuery.isXml(data)) {
258 // This fix ensures we have XML data
259 // Related to TreeStore calling getRoot with the root node, which isn't XML
260 // Probably should be resolved in TreeStore at some point
261 return Ext.DomQuery.selectNode(root, data);
267 * We're just preparing the data for the superclass by pulling out the record nodes we want
268 * @param {XMLElement} root The XML root node
269 * @return {Ext.data.Model[]} The records
271 extractData: function(root) {
272 var recordName = this.record;
276 Ext.Error.raise('Record is a required parameter');
280 if (recordName != root.nodeName) {
281 root = Ext.DomQuery.select(recordName, root);
285 return this.callParent([root]);
290 * See Ext.data.reader.Reader's getAssociatedDataRoot docs
291 * @param {Object} data The raw data object
292 * @param {String} associationName The name of the association to get data for (uses associationKey if present)
293 * @return {XMLElement} The root
295 getAssociatedDataRoot: function(data, associationName) {
296 return Ext.DomQuery.select(associationName, data)[0];
300 * Parses an XML document and returns a ResultSet containing the model instances
301 * @param {Object} doc Parsed XML document
302 * @return {Ext.data.ResultSet} The parsed result set
304 readRecords: function(doc) {
305 //it's possible that we get passed an array here by associations. Make sure we strip that out (see Ext.data.reader.Reader#readAssociated)
306 if (Ext.isArray(doc)) {
311 * @deprecated will be removed in Ext JS 5.0. This is just a copy of this.rawData - use that instead
316 return this.callParent([doc]);