PDA

View Full Version : XMLReader, complex xml for grid



jmuchrisf
2 Dec 2010, 7:13 PM
Good evening everyone!

All of the example EXTJs XML/grid examples use the Books, ISBN, ASIN number example. I have some XML that's a little more complex, and I don't know how to map these items to the grid fields. Any advice?

Examples I see:

<Item>
<ASIN>0446355453</ASIN>

<DetailPageURL>

http://www.amazon.com/gp/redirect.html%3FASIN=0446355453%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0446355453%253FSubscriptionId=1A7XKHR5BYD0WPJVQEG2

</DetailPageURL>

<ItemAttributes>
<Author>Sidney Sheldon</Author>
<Manufacturer>Warner Books</Manufacturer>
<ProductGroup>Book</ProductGroup>
<Title>Master of the Game</Title>
</ItemAttributes>
</Item>
maps like:

reader: new Ext.data.XmlReader({
// records will have an "Item" tag
record: 'Item',
id: 'ASIN',
totalRecords: '@total'
}, [
// set up the fields mapping into the xml doc
// The first needs mapping, the others are very basic
{name: 'Author', mapping: 'ItemAttributes > Author'},
'Title',
'Manufacturer',
'ProductGroup',
// Detail URL is not part of the column model of the grid
'DetailPageURL'
])

I have something like:


<buildings>
<building>
<floor name="firstfloor" display="First Floor">
<field_name name="sqfootage" display="100 sq ft"/>
<field_name name="height" display="9ft"/>
</floor>
<floor name="basement" display="Sub Zero Floor">
<field_name name="sqfootage" display="200 sq ft"/>
<field_name name="height" display="12ft"/>
</floor>
</building

</buildings>

That should get me going where I need to go. In Jquery, it's would map like this:
building.find('building") for each building..
then to get the first floor data it would be something like:
floor.find('floor[name="firstfloor"]').find('field_name[name="height"]').text() or somethingl ike that, to get the height of each floor..

how would I accomplish that in an XMLReader, to get a row for each 'building', and an attribute in each row that maps to the 'firstfloor' height attribute... and it has to be by 'value' of the 'name' attribute, not an index.

and I don't know how to map it.

Condor
3 Dec 2010, 3:33 AM
Firstly, what should your store contain? Should it contain buildings (with a 'floors' field) or floors (optionally extended with data from building)?

jmuchrisf
3 Dec 2010, 7:00 AM
Condor -
thanks for the response. While I see the difference in your two examples, I think the challenge is the same. the way I think about it is buildings with floors, and I only want information on certain floors.... yeah, that's right. I want a list of building supplemented with floor information (almost denormalized, etc)... I'm thinking of putting an XSLT in the middle of my program output and the extjs, but if extjs can do more complex (jquery DOM selector like) querying of XML, that would be great.

and the original XML I have is an industry standard format, so I can't change that.

Condor
3 Dec 2010, 7:47 AM
You could use nested readers, e.g.

var floorReader = new Ext.data.XmlReader({
record: 'floor',
fiels: [{
name: 'name',
mapping: '@name'
},{
name: 'display',
mapping: '@display'
},{
name: 'sqfootage',
mapping: 'field_name[name=sqfootage]@display'
},{
name: 'height',
mapping: 'field_name[name=height]@display'
}]
});
var buildingReader = new Ext.data.XmlReader({
record: 'building',
fields: [{
name: 'floors',
convert: function(v, n){
return floorReader.readRecords(n).records;
}
}]
});

Alexx2011
15 Nov 2011, 12:27 AM
Hi, i have the similar problem. My XML file is:


<?xml version="1.0" encoding="UTF-8"?>
<lakes>
<lake>
<lakeId>Lake1</lakeId>
<llrParameters LN="1167" LP="58" TotN="936" TotP="31" Q="7"></llrParameters>
<llrParameters LN="1310" LP="69" TotN="1006" TotP="41" Q="6"></llrParameters>
.............
</lake>
<lake>
<lakeId>Lake2</lakeId>
<llrParameters LN="1250" LP="55" TotN="999" TotP="35" Q="8"></llrParameters>
.............
</lake>
</lakes>


I have created store and nested XML readers:


Ext4.define('llrParametersXmlReader',{
extend: 'Ext4.data.XmlReader',
root: 'lake',
record: 'llrParameters',
fields: [{name: "LN", type: "float", mapping: '@LN'},
{name: "LP", type: "float", mapping: '@LP'},
{name: "TotN", type: "float", mapping: '@TotN'},
{name: "TotP", type: "float", mapping: '@TotP'},
{name: "Q", type: "float", mapping: '@Q'}
]
});
Ext4.define('lakeXmlReader',{
extend: 'Ext4.data.XmlReader',
root: 'lakes',
record: 'lake',
fields: [{name: "lakeId", type: "string"},
{name: 'llrParameters', convert: function(v, n)
{
return new Ext4.create('llrParametersXmlReader',{}).readRecords(n).records;
}
}]
});
//(I'm using Ext4,create because I'm working into sandbox)
//The store is:
Ext4.define('LlrParametersStore', {
extend: 'Ext4.data.Store',
autoLoad: true,
model: 'LlrParametersModel',
proxy: {
type: 'ajax',
url : 'xml/LlrParametersXmlStore.xml',
reader: new Ext4.create('lakeXmlReader',{})
}
});


This store is the store for a grid. I expect to see values of llrParameters (LN, LP etc) into the grid, but I have seen just zeros (grid contains 2 row - for "lake1" and "lake2" with zeros instead real values of llrParameters).
The grid is like:


var resultsStore = Ext4.create('LlrParametersStore',{});
var grid = Ext4.create('Ext4.grid.Panel', {
store: resultsStore,
selType: 'rowmodel',
frame: true,
autoScroll: true,
columns:[
{ header: "LN",
dataIndex: "LN",
width: 130
},
..........
});


How to show values? (Actually, I want to show parameters for chosen lake only)

Alexx2011
15 Nov 2011, 1:09 AM
Additionally,
When my model is:


Ext4.define('LlrParametersModel', {
extend: 'Ext4.data.Model',
fields: [{name: "lakeId", type: "string"},
{name: "LN", type: "float", mapping: 'llrParameters > @LN'},
{name: "LP", type: "float", mapping: 'llrParameters > @LP'},
{name: "TotN", type: "float", mapping: 'llrParameters > @TotN'},
{name: "TotP", type: "float", mapping: 'llrParameters > @TotP'},
{name: "Q", type: "float", mapping: 'llrParameters > @Q'}
]
});


I can see values of llr parameters, but still just one row (it should be several)

anand.kochar
30 Nov 2011, 12:03 AM
Hi Condor,

I am trying to bind below xml to XMLstore but not able to do so successfully.
Can you please help me with this.

XML :
<?xml version="1.0" encoding="utf-8" ?>
<OpCh>
- <quot>
<p key="datax">valuex</p>
<p key="datay">valuey</p>
</quot>
- <op>
<p key="data1">value11</p>
<p key="data2">value12</p>
</op>
- <op>
<p key="data1">value21</p>
<p key="data2">value22</p>
</op>
<count>2</count>
<aKey>123</AccessKey>
</OpCh>

XML Store:
XMLStore = new Ext.data.XmlStore({
// store configs
autoDestroy: true,
storeId: 'myXmlStore',
reader: new Ext.data.XmlReader({
record: 'OpCh',
totalRecords : '@count'
},
[
// set up the fields mapping into the xml doc
// The first needs mapping, the others are very basic
{name: 'data1', mapping: 'op > data1'},
{name: 'data2', mapping: 'op > data2'},

])
});

StudentDaniel
11 Jan 2013, 4:45 AM
Hello Condor,

because of there is no new comment for more than a year I want to ask you or another Sencha-Support Agent for a final Answer to this thread.

How can I use the Sencha Architect 2 to WORK with XML ?

http://www.w3schools.com/xml/xml_attributes.asp


<person sex="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

A need to do read/write XML within Attributs and Elements.

Is there a way working with Stores->Proxys->Reader/Writer within the Architect 2 ?
Maybe you can test if there is a useable way with a short example like above.

Hope you don't have bad information like "sencha dose only work with simple XML without Attributs and Elements"... ~o)

I will also ask a second Question about "wadl" as separated thread.

I am testing Sencha the next 4 Weeks and are planing to buy Sencha Complete.

I hope this Investment will be the right way for a student... :-)