PDA

View Full Version : retrieving array using Xmlreader



nayak14
17 Sep 2009, 11:49 PM
Hi,
I am retrieving a record from database having following record format.

..........
<items>
<item>1</item>
<item>2</item>
</items>
...........

when i try to retrieve this array using record.data.items it says it undefined.
How can we retrieve the array information using xmlreader?

Please let me know ASAP.

Condor
18 Sep 2009, 12:33 AM
reader: new Ext.data.XmlReader({
record: 'item',
idProperty: '/',
fields: [{name: 'field1', mapping: '/'}]
})

nayak14
18 Sep 2009, 12:50 AM
thanks for you reply
but the xml piece mentioned above is not the complete record xml.
the array is the part of a record xml

like
<record>
.........
<items>
<item>1</item>
<item>2</item>
</items>
............
</record>

now i am able to retrieve all other fields from the records using record.data.<fieldname>
but not the arrray using record.data.items

nayak14
18 Sep 2009, 1:46 AM
I think, what i need over here is how to define an array field while creating xmlreader object.

Normal way is
new Ext.data.XmlReader({
id: 'xyz',
record: 'record'
},
[
{name: 'id'},
{name: 'name'},
{name: 'uri'},
{name: 'description'}
]
});
what if i have an array element in the record.
how should i define the field?

Condor
18 Sep 2009, 3:28 AM
You would need to use a convert function, e.g.

{name: 'items', mapping: 'items', convert: function(v){
var nodes = Ext.DomQuery.select('item', v), items = [];
for(var i = 0, len = nodes.length; i < len; i++){
var node = nodes[i];
items.push(node.firstChild ? node.firstChild.nodeValue : null);
}
return items;
}}

nayak14
18 Sep 2009, 4:40 AM
Thanks for the code.
But it seems its not workig.......
i am getting v as null
and nodes.length = 0

let me know where is it going wrong?

nayak14
18 Sep 2009, 5:02 AM
The exact structure is as given below.<items><item> <itemId>100</itemId> <itemName>1</itemName></item><item> <itemId>101</itemId> <itemName>1</itemName></item></items>I want to retrieve all the "item"s in an array for processing.appreciate your help...

Condor
18 Sep 2009, 6:46 AM
You're confusing me...

It this part of a larger document and do you want to read these items in a field or is this the entire document and do you want to read this in a store?

nayak14
18 Sep 2009, 10:23 AM
Sorry.... let me make it clear.
the complete xml is as follows.

<itemDetails>
<vendorId>123</vendorId>
<locationId></locationId>
<items>
<item>
<itemId>100</itemId>
<itemName>1</itemName>
</item>
<item>
<itemId>101</itemId>
<itemName>1</itemName>
</item>
</items>
</itemDetails>

No i want to read it in a store using xmlreader.
in normal scenario with basic field definition we can access any field as record.data.vendorId,record.data.locationId......
similiraly, i want to access the array of items so i can access them as record.data.items[0].id,record.data.items[0].name
Is it possible anyway?

I hope i am clear now.........
let me know if u need any other info.

Thanks a bunch!!

Condor
19 Sep 2009, 1:43 AM
This would create a field 'items' that holds an array of {itemId, itemName} objects:

{name: 'items', mapping: 'items', convert: function(v){
var nodes = Ext.DomQuery.select('item', v), items = [];
for(var i = 0, len = nodes.length; i < len; i++){
var node = nodes[i];
items.push({
itemId: Ext.DomQuery.selectValue('itemId', node),
itemName: Ext.DomQuery.selectValue('itemName', node)
});
}
return items;
}}

nayak14
21 Sep 2009, 3:18 AM
Thanks for the code.
But as i mentioned earlier i a receiving V as empty string.
also getting nodes.length as 0.

do i need any upgrade or changed version of ext-all.js?


Pls. help.

Condor
21 Sep 2009, 3:53 AM
My example should work on all Ext versions.

Can you post your current XmlReader config and complete XML data?

nayak14
21 Sep 2009, 4:16 AM
give me ur email address
i will send u the details

Condor
21 Sep 2009, 4:19 AM
You could send the info using a private forum message or by uploading it to some publicly available server (port 80 only - I'm behind a firewall).

nayak14
21 Sep 2009, 4:30 AM
just sent u private message with the xml data, field definition and xmlreader definition.

thanks

Condor
21 Sep 2009, 4:48 AM
I just realized my mistake. You can't use 'mapping' for this problem. You'll have to use:

{name: 'costCentres', convert: function(v, n){
var nodes = Ext.DomQuery.select('costCentres > costCentre', n), costCentres = [];
for(var i = 0, len = nodes.length; i < len; i++){
var node = nodes[i];
costCentres.push({
allocation: Ext.DomQuery.selectValue('> costCentreAllocation', node),
id: Ext.DomQuery.selectValue('> costCentreId', node)
});
}
return costCentres;
}}

nayak14
21 Sep 2009, 4:53 AM
Bingo!!!!
Its Working......
thanks a lot!!!!!

one suggestion can we have this implementation by default in ExtJs??
cause this would be common query for ExtJs users.


thank you very much.
Regards,
Harshad

nayak14
21 Sep 2009, 5:00 AM
still one problem.
i am not able to retrieve the inner fields using
record.data.costCentres[0].costCentreId

nayak14
21 Sep 2009, 5:01 AM
sorry got the problem.
i should use id and not costcentreid
sorry or bothering u