PDA

View Full Version : Ext Js4 - Directly load xml data to the xml store works improperly?



moneta
20 Jul 2012, 2:18 AM
Hi all,
I got problem while directly loading the xml data into xml store. Maybe I am missing something.

Here goes my code:

1. Define model:

Ext.define('MoneFox.model.SchemaRelation', {
extend: 'Ext.data.Model',

fields: [
{
name: 'fromcode',
type: 'string'
},
{
name: 'lookup',
type: 'string'
}
]
});

2. Define store:

Ext.define('MoneFox.store.SchemaRelations', {
extend: 'Ext.data.Store',
requires: [
'MoneFox.model.SchemaRelation'
],

constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
storeId: 'SchemaRelations',
model: 'MoneFox.model.SchemaRelation',
proxy: {
type: 'memory',
reader: {
type: 'xml',
record: 'link'
},
writer: {
type: 'xml',
record: 'link'
}
}
}, cfg)]);
}
});

3. XML Data:


<Schema>
<link>
<fromcode>ACTIVI</fromcode>
<lookup>(Select * from Activity where Type='A')</lookup>
</link>
<link>
<fromcode>ACTIVI\MARCAP</fromcode>
<lookup>ACTIVI.MarCode = MARCAP.Code</lookup>
</link>
<link>
<fromcode>ACTIVI\EMPL</fromcode>
<lookup>ACTIVI.EmpCode = EMPL.Code</lookup>
</link>
<field>
<node>MarCode</node>
<table>ACTIVI</table>
<name>MarCode</name>
</field>
</Schema>


4. Directly load xml store with xml data using loadRawData:

var store = Ext.data.StoreManager.lookup('SchemaRelations');

store.on('load', function(){
alert(store.getTotalCount());
});

store.loadRawData("<Schema> <link> <fromcode>ACTIVI</fromcode> <lookup>(Select * from Activity where Type='A')</lookup> </link> <link> <fromcode>ACTIVI\MARCAP</fromcode> <lookup>ACTIVI.MarCode = MARCAP.Code</lookup> </link> <link> <fromcode>ACTIVI\EMPL</fromcode> <lookup>ACTIVI.EmpCode = EMPL.Code</lookup> </link> <field> <node>MarCode</node> <table>ACTIVI</table> <name>MarCode</name> </field></Schema>");

console.log(store.data);



5. And here are weird results:
- The alert on the event store on load display getTotalCount of 384 ?!?
- The console.log shows object having 02 items, but all items have empty value: fromcode = "" and lookup = "" ?!?

Pls. anyone could kindly help. Thanks.

sword-it
20 Jul 2012, 3:43 AM
Hi moneta,

problem in your reader config -



proxy: {
type: 'memory',
reader: {
type: 'xml',
record: 'link' // It's root: 'link'
},
writer: {
type: 'xml',
record: 'link' // It's root: 'link'
} }


record is not valid config it's "root" .

moneta
20 Jul 2012, 4:43 AM
Hi Sword,
Thanks a lot for your reply. I have just tried to reconfigured it to root: 'link but it doesn't work.

I think the root should be 'Schema' and the record is still 'link'. Because I did try to create another store folowing instruction in http://docs.sencha.com/ext-js/4-1/#!/example/grid/xml-grid.html , type ajax with url point to the the file containing this xml, then it worked perfectly.

It looks like that the reader have somehow reads the xml but fail to map it to memory store data ? The getTotalCount = 384 is actually the strlen of the xml data string.

Best,
Moneta

moneta
23 Jul 2012, 12:01 AM
Anyone could help on this, please ?

evant
23 Jul 2012, 12:26 AM
You're passing it a string, it's expecting an XML document.

moneta
23 Jul 2012, 1:08 AM
How can I do that? I tried this but it doesn't work.


store.loadRawData("<?xml version="1.0" encoding="UTF-8"?>
<Schema> <link> <fromcode>ACTIVI</fromcode> <lookup>(Select * from Activity where Type='A')</lookup> </link> <link> <fromcode>ACTIVI\MARCAP</fromcode> <lookup>ACTIVI.MarCode = MARCAP.Code</lookup> </link> <link> <fromcode>ACTIVI\EMPL</fromcode> <lookup>ACTIVI.EmpCode = EMPL.Code</lookup> </link> <field> <node>MarCode</node> <table>ACTIVI</table> <name>MarCode</name> </field></Schema>");

moneta
25 Jul 2012, 12:14 AM
I stuck with this one for serveral days. Anyone could help, please?

moneta
26 Jul 2012, 3:35 AM
I got it worked. Just like to share with you if you might get the same issues. The code is just sample like this:


data = '<?xml version="1.0" encoding="UTF-8"?>
<Schema> <link> <fromcode>ACTIVI</fromcode> <lookup>(Select * from Activity where Type='A')</lookup> </link> <link> <fromcode>ACTIVI\MARCAP</fromcode> <lookup>ACTIVI.MarCode = MARCAP.Code</lookup> </link> <link> <fromcode>ACTIVI\EMPL</fromcode> <lookup>ACTIVI.EmpCode = EMPL.Code</lookup> </link> <field> <node>MarCode</node> <table>ACTIVI</table> <name>MarCode</name> </field></Schema>';


if ( window.DOMParser ) { // Standard
tmp = new DOMParser();
xml = tmp.parseFromString( data , "text/xml" );
} else { // IE
xml = new ActiveXObject( "Microsoft.XMLDOM" );
xml.async = "false";
xml.loadXML( data );
}


console.log(xml); // --> It shows "document" ;-)

store.loadRawData(xml);


Thanks to the post in http://stackoverflow.com/questions/4319181/returning-xml-string-how-to-parse-xml-file-using-jquery-json

Best,
Moneta