Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / data / writer / Xml.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
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.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @author Ed Spencer
17  * @class Ext.data.writer.Xml
18  * @extends Ext.data.writer.Writer
19
20 This class is used to write {@link Ext.data.Model} data to the server in an XML format.
21 The {@link #documentRoot} property is used to specify the root element in the XML document.
22 The {@link #record} option is used to specify the element name for each record that will make
23 up the XML document.
24
25  * @markdown
26  */
27 Ext.define('Ext.data.writer.Xml', {
28     
29     /* Begin Definitions */
30     
31     extend: 'Ext.data.writer.Writer',
32     alternateClassName: 'Ext.data.XmlWriter',
33     
34     alias: 'writer.xml',
35     
36     /* End Definitions */
37     
38     /**
39      * @cfg {String} documentRoot The name of the root element of the document. Defaults to <tt>'xmlData'</tt>.
40      * If there is more than 1 record and the root is not specified, the default document root will still be used
41      * to ensure a valid XML document is created.
42      */
43     documentRoot: 'xmlData',
44     
45     /**
46      * @cfg {String} defaultDocumentRoot The root to be used if {@link #documentRoot} is empty and a root is required
47      * to form a valid XML document.
48      */
49     defaultDocumentRoot: 'xmlData',
50
51     /**
52      * @cfg {String} header A header to use in the XML document (such as setting the encoding or version).
53      * Defaults to <tt>''</tt>.
54      */
55     header: '',
56
57     /**
58      * @cfg {String} record The name of the node to use for each record. Defaults to <tt>'record'</tt>.
59      */
60     record: 'record',
61
62     //inherit docs
63     writeRecords: function(request, data) {
64         var me = this,
65             xml = [],
66             i = 0,
67             len = data.length,
68             root = me.documentRoot,
69             record = me.record,
70             needsRoot = data.length !== 1,
71             item,
72             key;
73             
74         // may not exist
75         xml.push(me.header || '');
76         
77         if (!root && needsRoot) {
78             root = me.defaultDocumentRoot;
79         }
80         
81         if (root) {
82             xml.push('<', root, '>');
83         }
84             
85         for (; i < len; ++i) {
86             item = data[i];
87             xml.push('<', record, '>');
88             for (key in item) {
89                 if (item.hasOwnProperty(key)) {
90                     xml.push('<', key, '>', item[key], '</', key, '>');
91                 }
92             }
93             xml.push('</', record, '>');
94         }
95         
96         if (root) {
97             xml.push('</', root, '>');
98         }
99             
100         request.xmlData = xml.join('');
101         return request;
102     }
103 });
104