View Full Version : How to show xml data in grid?

1 Mar 2010, 3:59 AM
I have an xml data that I want to show in grid. My xml looks like this:

<Status DateAndTime='20-11-2010'>
<Door State='1' Description='sadsaasd' />
<Acc State='1' Description='sadsaasd' />
<Status DateAndTime='10-10-2009'>
<Door State='1' Description='sadsaasd' />
<Acc State='1' Description='sadsaasd' />

Now I want to show this kind of data in my grid...I m confused how to map each <Status> tag, its attribute DateAndTime and its inner Elements???

Can anyone give me a little code or hint to start with? I want to have customer renderer for to decide whether which image should be inserted with data depending upon State attribute values, that is if it will be '1' then some img otherwise some other img.....!!!

1 Mar 2010, 4:17 AM
The following should do it for you:

var store = new Ext.data.Store(
url: 'filename.xml',
reader: new Ext.data.XmlReader(
record: 'Status'
}, [
{name: 'DateAndTime', mapping: '@DateAndTime'},
{name: 'DoorState', mapping: 'Door > @State'},
{name: 'DoorDescription', mapping: 'Door > @Description'},
{name: 'AccState', mapping: 'Acc > @State'},
{name: 'AccDescription', mapping: 'Acc > @Description'}

1 Mar 2010, 9:48 AM
Fay, if I try to make it dynamic. Like if I dont know the names of all elements inside each <Status> tag, then what approach would it be?

I have tried it doing like this:

var recData =[];
recData.push(new Ext.data.Field({name: 'DateAndTime', mapping: '@DateAndTime'})); // hard code for testing
var statusStore = Ext.StoreMgr.get('statusStore');
var newRecord = new statusStore.recordType(recData);
statusStore.fields = newRecord;

But nothing is showing up in grid. Please help!

1 Mar 2010, 11:38 AM
So what you originally said you wanted isn't actually what you wanted? Now the fields are dynamic? You're probably better off looking at JsonReader and metaData - or taking the source code for this and re-implementing it for XmlReader. See also: http://www.extjs.com/forum/showthread.php?t=4548

1 Mar 2010, 12:57 PM
No, I have got what I originally wanted. Thanks a lot Fay...!
I just wanted to have a clarification about my second question as well. Anyway, I have done it by they way. Thanks