PDA

View Full Version : XMLStore to select a specific node



kathir
12 Feb 2014, 6:19 PM
I have a XML file with the following schema



<root>
<item>
<subitem name="parent">
<list>
<subitem name="c1">
<subitem name="c2">
<subitem name="c3">
</list>
</item>
</root>


The issue for me here is the subitem is under two parent nodes. I would like to construct the model with subitem under the item as the parent and all the remaining subitems under list as the children of that. Some thing like below



Item
name:"parent"
children : [ itemc1,itemc2,itemc3]


IS this possible using XMLStore. When i use item as the root node and subitem as the record node. The store is pulling all the subitems.

Appreciate your help.

mitchellsimoens
20 Feb 2014, 7:57 AM
You could use associations:


Ext.define('MyModel', {
extend : 'Ext.data.Model',

fields : [
{ name : 'name', mapping : 'subitem@name' }
],

hasMany : {
model : 'SubModel',
name : 'list',
associationKey : 'list',
reader : {
type : 'xml',
root : 'list',
record : 'subitem'
}
}
});

Ext.define('SubModel', {
extend : 'Ext.data.Model',

fields : [
{ name : 'name', mapping : '@name' }
]
});

new Ext.data.Store({
autoLoad : true,
model : 'MyModel',
proxy : {
type : 'ajax',
url : '/data/xml.xml',
reader : {
type : 'xml',
root : 'root',
record : 'item'
}
},
listeners : {
load : function(store, records) {
var record = records[0],
list = record.list();

console.log(list);
}
}
});

Of course your sample XML is incorrect:


<root>
<item>
<subitem name="parent" />
<list>
<subitem name="c1" />
<subitem name="c2" />
<subitem name="c3" />
</list>
</item>
</root>

kathir
20 Feb 2014, 3:19 PM
Hi,
This case works exactly the way i expect.


<root>
<item>
<subitem name="parent" />
<list>
<subitem name="c1" />
<subitem name="c2" />
<subitem name="c3" />
</list>
</item>
</root>

I also have to parse a nested structure of the same xml like below,



<root>
<item>
<subitem name="parent" />
<list>
<subitem name="c1">
<list>
<subitem name="c1c1">
<list>
<subitem name="c1c1c1"/>
</list>
</subitem>
<subitem name="c1c2"/>
</list>
</subitem>
<subitem name="c2" />
<subitem name="c3" />
</list>
</item>
</root>


I would like to store each subitem with its list as a nested structure. The XmlStore with the above configuration does not works because of the nesting. How should i use the store readers with this structure.