PDA

View Full Version : [FIXED] Nested XML duplicate field issue



swardson
23 Feb 2012, 2:49 PM
Hey all,

I seem to have run across a potential bug when reading nested XML.

Here is a sample xml structure:



<?xml version="1.0" encoding="utf-8"?>

<data>
<items>
<item>
<subitems>
<subitem>
<title>A Subtitle</title>
</subitem>
<subitem>
<title>Another Subtitle</title>
</subitem>
</subitems>
<title>Some Title</title>
</item>
<item>
<subitems>
<subitem>
<title>A Subtitle</title>
</subitem>
<subitem>
<title>Another Subtitle</title>
</subitem>
</subitems>
<title>Some Title</title>
</item>
</items>
</data>


Notice how we have title tags nested within subitem and one under each item. However, the title tag that is a direct child of item is listed below the nested sub items.

When I read this structure with associations and want to get the <title> of <item> the data being returned to the field in the model is the first title in the list of subitems rather than the title tag in item.

Here is a sample store:



app.stores.testStore = Ext.regStore('testStore', {
model: app.models.test,

proxy: {
type: 'ajax',
url: 'path/to/test.xml',

reader: {
type: 'xml',
root: 'items',
record: 'item'
},
},
});


And here is the models with associations:



app.models.subitem = Ext.regModel('subitem', {

fields: [
{name: 'title', type: 'string'},
],

belongsTo: 'test',

proxy: {
type: 'memory',

reader: {
type: 'xml',
root: 'subitems',
record: 'subitem'
},
},
});

app.models.test = Ext.regModel('test', {
fields: [
{name: 'title', type: 'string'}
],

hasMany: ['SubItems', {model: 'subitem', name: 'subitems'}],
});


When I use the code below to read the data for the first <item> and subseuqently its <title> I am getting a value of "A Subtitle" which is the title of the first subitem rather then the expected "A Title" which is the title of the first item.



var test = app.stores.test;
test.load();

test.on('load', function(){
console.log(test.getAt(0).data.title);
}


Am I doing something wrong or is this a bug?

swardson
23 Feb 2012, 4:02 PM
I wanted to mention that I could solve this by placing the <title> tag for item above the <subitems> tag, however, this XML structure in my case is predefined and not something I can edit prior to loading.

Thanks!

mitchellsimoens
24 Feb 2012, 9:53 AM
Looks like first one first served no matter the level.

swardson
24 Feb 2012, 10:44 AM
Yup,

The only workaround I can think of at the moment is to create a proxy to read all of the <title> elements and then test if the parent_id is the correct one before using it. For deeply nested xml this adds quite a bit of unneeded parsing and I haven't implemented it yet, but it's an idea.

Any idea if this has been (or will be) fixed in Sencha v2.0?

Thanks!