PDA

View Full Version : Mapping results in faulty XML



JohnZ
18 Jan 2010, 8:10 AM
Hi all,
I'm trying to read a complicated XML from a server for which I created a reader like so:


var Reader = new Ext.data.XmlReader({
root: "data",
record: 'member/value/array/data/value',
idProperty: "value:nth-child(2)/string",
fields: [
{name: 'name', type: 'string', mapping: 'value:nth-child(1)/string'},
{name: 'id', type: 'string', mapping: 'value:nth-child(2)/string'},
{name: 'password', type: 'string', mapping: 'value:nth-child(3)/string'}
]
});

This works perfectly, I can show the data in a grid.

I created an XMLWriter as well:


var Writer = new Ext.data.XmlWriter({
encode: true
});


The XMLWriter uses the mapping instead of the names and sends data to the server like this which IMHO this is not correct XML:


<?xml version="1.0" encoding="ISO-8859-15"?>
<member/value/array/data/value>
<value:nth-child(1)/string>JOHN</value:nth-child(1)/string>
<value:nth-child(2)/string>2</value:nth-child(2)/string>
</member/value/array/data/value>


I would expect this:


<?xml version="1.0" encoding="ISO-8859-15"?>
<data>
<name>JOHN</name>
<id>2</id>
</data>


Am I doing something wrong or is this an error?

ext.js 3.1.0

JohnZ
19 Jan 2010, 4:54 AM
Turns out that this is documented behaviour, call me stupid.
Has anyone used it like this before? Do you have different solution than this:

This is how I solved it (commented lines were changes from the original ext.js functions):


Ext.override(Ext.data.XmlWriter, {
toHash : function(rec, config) {
var map = rec.fields.map,
data = {},
raw = (this.writeAllFields === false && rec.phantom === false) ? rec.getChanges() : rec.data,
m;
Ext.iterate(raw, function(prop, value){
if((m = map[prop])){
//data[m.mapping ? m.mapping : m.name] = value;
data[m.name] = value;
}
});
return data; //Add a return seems to be necessary
},
render : function(params, baseParams, data) {
baseParams = this.toArray(baseParams);
params.xmlData = this.tpl.applyTemplate({
version: this.xmlVersion,
encoding: this.xmlEncoding,
documentRoot: (baseParams.length > 0 || this.forceDocumentRoot === true) ? this.documentRoot : false,
//record: this.meta.record,
record: this.meta.root,
root: this.root,
baseParams: baseParams,
records: (Ext.isArray(data[0])) ? data : [data]
});
}
});


I wonder if anyone is interested in this problem at all...:((

Condor
19 Jan 2010, 4:57 AM
The easiest method is not to override XmlWriter, but to specify a 'tpl' config option.