]>
git.proxmox.com Git - sencha-touch.git/blob - src/src/data/reader/Xml.js
3 * @class Ext.data.reader.Xml
4 * @extends Ext.data.reader.Reader
6 * The XML Reader is used by a Proxy to read a server response that is sent back in XML format. This usually
7 * happens as a result of loading a Store - for example we might create something like this:
10 * extend: 'Ext.data.Model',
12 * fields: ['id', 'name', 'email']
16 * var store = Ext.create('Ext.data.Store', {
28 * The example above creates a 'User' model. Models are explained in the {@link Ext.data.Model Model} docs if you're
29 * not already familiar with them.
31 * We created the simplest type of XML Reader possible by simply telling our {@link Ext.data.Store Store}'s
32 * {@link Ext.data.proxy.Proxy Proxy} that we want a XML Reader. The Store automatically passes the configured model to the
33 * Store, so it is as if we passed this instead:
41 * The reader we set up is ready to read data from our server - at the moment it will accept a response like this:
43 * <?xml version="1.0" encoding="UTF-8"?>
47 * <name>Ed Spencer</name>
48 * <email>ed@sencha.com</email>
52 * <name>Abe Elias</name>
53 * <email>abe@sencha.com</email>
57 * The XML Reader uses the configured {@link #record} option to pull out the data for each record - in this case we
58 * set record to 'user', so each `<user>` above will be converted into a User model.
60 * ## Reading other XML formats
62 * If you already have your XML format defined and it doesn't look quite like what we have above, you can usually
63 * pass XmlReader a couple of configuration options to make it parse your format. For example, we can use the
64 * {@link #rootProperty} configuration to parse data that comes back like this:
66 * <?xml version="1.0" encoding="UTF-8"?>
70 * <name>Ed Spencer</name>
71 * <email>ed@sencha.com</email>
75 * <name>Abe Elias</name>
76 * <email>abe@sencha.com</email>
80 * To parse this we just pass in a {@link #rootProperty} configuration that matches the 'users' above:
85 * rootProperty: 'users'
88 * Note that XmlReader doesn't care whether your {@link #rootProperty} and {@link #record} elements are nested deep
89 * inside a larger structure, so a response like this will still work:
91 * <?xml version="1.0" encoding="UTF-8"?>
98 * <name>Ed Spencer</name>
99 * <email>ed@sencha.com</email>
103 * <name>Abe Elias</name>
104 * <email>abe@sencha.com</email>
111 * ## Response metadata
113 * The server can return additional data in its response, such as the {@link #totalProperty total number of records}
114 * and the {@link #successProperty success status of the response}. These are typically included in the XML response
117 * <?xml version="1.0" encoding="UTF-8"?>
120 * <success>true</success>
123 * <name>Ed Spencer</name>
124 * <email>ed@sencha.com</email>
128 * <name>Abe Elias</name>
129 * <email>abe@sencha.com</email>
133 * If these properties are present in the XML response they can be parsed out by the XmlReader and used by the
134 * Store that loaded it. We can set up the names of these properties by specifying a final pair of configuration
139 * rootProperty: 'users',
140 * totalProperty : 'total',
141 * successProperty: 'success'
144 * These final options are not necessary to make the Reader work, but can be useful when the server needs to report
145 * an error or if it needs to indicate that there is a lot of data available of which only a subset is currently being
150 * __Note:__ In order for the browser to parse a returned XML document, the Content-Type header in the HTTP
151 * response must be set to "text/xml" or "application/xml". This is very important - the XmlReader will not
152 * work correctly otherwise.
154 Ext
. define ( 'Ext.data.reader.Xml' , {
155 extend
: 'Ext.data.reader.Reader' ,
156 alternateClassName
: 'Ext.data.XmlReader' ,
161 * @cfg {String} record The DomQuery path to the repeated element which contains record information.
168 * Creates a function to return some particular key of data from a response. The {@link #totalProperty} and
169 * {@link #successProperty} are treated as special cases for type casting, everything else is just a simple selector.
170 * @param {String} expr
173 createAccessor : function ( expr
) {
176 if ( Ext
. isEmpty ( expr
)) {
180 if ( Ext
. isFunction ( expr
)) {
184 return function ( root
) {
185 return me
. getNodeValue ( Ext
. DomQuery
. selectNode ( expr
, root
));
189 getNodeValue : function ( node
) {
190 if ( node
&& node
. firstChild
) {
191 return node
. firstChild
. nodeValue
;
197 getResponseData : function ( response
) {
198 // Check to see if the response is already an xml node.
199 if ( response
. nodeType
=== 1 || response
. nodeType
=== 9 ) {
203 var xml
= response
. responseXML
;
208 * @event exception Fires whenever the reader is unable to parse a response.
209 * @param {Ext.data.reader.Xml} reader A reference to this reader.
210 * @param {XMLHttpRequest} response The XMLHttpRequest response object.
211 * @param {String} error The error message.
213 this . fireEvent ( 'exception' , this , response
, 'XML data not found in the response' );
215 Ext
. Logger
. warn ( 'XML data not found in the response' );
223 * Normalizes the data object.
224 * @param {Object} data The raw data object.
225 * @return {Object} Returns the `documentElement` property of the data object if present, or the same object if not.
227 getData : function ( data
) {
228 return data
. documentElement
|| data
;
233 * Given an XML object, returns the Element that represents the root as configured by the Reader's meta data.
234 * @param {Object} data The XML data object.
235 * @return {XMLElement} The root node element.
237 getRoot : function ( data
) {
238 var nodeName
= data
. nodeName
,
239 root
= this . getRootProperty ();
241 if (! root
|| ( nodeName
&& nodeName
== root
)) {
243 } else if ( Ext
. DomQuery
. isXml ( data
)) {
244 // This fix ensures we have XML data
245 // Related to TreeStore calling getRoot with the root node, which isn't XML
246 // Probably should be resolved in TreeStore at some point
247 return Ext
. DomQuery
. selectNode ( root
, data
);
253 * We're just preparing the data for the superclass by pulling out the record nodes we want.
254 * @param {XMLElement} root The XML root node.
255 * @return {Ext.data.Model[]} The records.
257 extractData : function ( root
) {
258 var recordName
= this . getRecord ();
262 Ext
. Logger
. error ( 'Record is a required parameter' );
266 if ( recordName
!= root
. nodeName
&& recordName
!== root
. localName
) {
267 root
= Ext
. DomQuery
. select ( recordName
, root
);
271 return this . callParent ([ root
]);
276 * See {@link Ext.data.reader.Reader#getAssociatedDataRoot} docs.
277 * @param {Object} data The raw data object.
278 * @param {String} associationName The name of the association to get data for (uses {@link Ext.data.association.Association#associationKey} if present).
279 * @return {XMLElement} The root.
281 getAssociatedDataRoot : function ( data
, associationName
) {
282 return Ext
. DomQuery
. select ( associationName
, data
)[ 0 ];
286 * Parses an XML document and returns a ResultSet containing the model instances.
287 * @param {Object} doc Parsed XML document.
288 * @return {Ext.data.ResultSet} The parsed result set.
290 readRecords : function ( doc
) {
291 //it's possible that we get passed an array here by associations. Make sure we strip that out (see Ext.data.reader.Reader#readAssociated)
292 if ( Ext
. isArray ( doc
)) {
295 return this . callParent ([ doc
]);
300 * Returns an accessor expression for the passed Field from an XML element using either the Field's mapping, or
301 * its ordinal position in the fields collection as the index.
303 * This is used by `buildExtractors` to create optimized on extractor function which converts raw data into model instances.
305 createFieldAccessExpression : function ( field
, fieldVarName
, dataName
) {
306 var selector
= field
. getMapping () || field
. getName (),
309 if ( typeof selector
=== 'function' ) {
310 result
= fieldVarName
+ '.getMapping()(' + dataName
+ ', this)' ;
312 selector
= selector
. split ( '@' );
314 if ( selector
. length
=== 2 && selector
[ 0 ]) {
315 result
= 'me.getNodeValue(Ext.DomQuery.selectNode("@' + selector
[ 1 ] + '", Ext.DomQuery.selectNode("' + selector
[ 0 ] + '", ' + dataName
+ ')))' ;
316 } else if ( selector
. length
=== 2 ) {
317 result
= 'me.getNodeValue(Ext.DomQuery.selectNode("@' + selector
[ 1 ] + '", ' + dataName
+ '))' ;
318 } else if ( selector
. length
=== 1 ) {
319 result
= 'me.getNodeValue(Ext.DomQuery.selectNode("' + selector
[ 0 ] + '", ' + dataName
+ '))' ;
321 throw "Unsupported query - too many queries for attributes in " + selector
. join ( '@' );