PDA

View Full Version : Ext.data.writer.Xml not escaping reserved characters



spcjak7221
11 Aug 2011, 9:15 AM
Ext.data.writer.Xml doesn't escape reserved characters "%", "&", "<", and ">" iaw: http://msdn.microsoft.com/en-us/library/ms145315(v=sql.90).aspx

(http://msdn.microsoft.com/en-us/library/ms145315(v=sql.90).aspx)This causes problems when attempting to use it with the .Net Framework and possibly other XML parsers.

Fix (may not be the most efficient method but it works):



writeRecords: function(request, data) {
var me = this,
xml = [],
i = 0,
len = data.length,
root = me.documentRoot,
record = me.record,
needsRoot = data.length !== 1,
item,
key;

// may not exist
xml.push(me.header || '');

if (!root && needsRoot) {
root = me.defaultDocumentRoot;
}

if (root) {
xml.push('<', root, '>');
}

for (; i < len; ++i) {
item = data[i];
xml.push('<', record, '>');
for (key in item) {
if (item.hasOwnProperty(key)) {
var zItem = item[key];
if((typeof zItem)=='string'){
zItem = zItem.replace('&','&amp;')
.replace('>','&gt;')
.replace('<','&lt;')
.replace('%','%');
}
xml.push('<', key, '>', zItem, '</', key, '>');
}
}
xml.push('</', record, '>');
}

if (root) {
xml.push('</', root, '>');
}

request.xmlData = xml.join('');
return request;
}
});

spcjak7221
11 Aug 2011, 12:25 PM
Update
The search / replace needs to be global.



if((typeof zItem)=='string'){
zItem = zItem.replace(/&/g,'&amp;')
.replace(/>/g,'&gt;')
.replace(/</g,'&lt;')
.replace(/%/g,'%');
}