PDA

View Full Version : XML reader for nested entities



cbockermann
7 Aug 2010, 9:44 AM
Hi,

I'm trying to use the XMLReader in Sencha Touch 0.9.3 to read XML data that looks like this:


<network>
<node>
<progID>90000032018A0401</progID>
<nodeID>01005B0FE600</nodeID>
<deviceClass>digitalIO</deviceClass>
<nodeName>Digitale Ausgänge (Relais)</nodeName>
<variables>
<variable>
<varName>Relais 1</varName>
<varType>bool</varType>
<readOnly>0</readOnly>
</variable>
<variable>
<varName>Relais 2</varName>
<varType>bool</varType>
<readOnly>1</readOnly>
</variable>
<variable>
<varName>Relaistemperatur</varName>
<varType>float</varType>
<varUnit>°C</varUnit>
<readOnly>0</readOnly>
</variable>
</variables>
</node>
</network>i.e. I have a list of nodes that each contain a number of variables. Currently I can read the nodes but so haven't succeeded in reading the nested elements. Here's my code:



Ext.regModel('Node', {
fields : [{
name : 'progID',
type : 'string'
}, {
name : 'nodeID',
type : 'string'
}, {
name : 'deviceClass',
type : 'string'
}, {
name : 'nodeName',
type : 'string'
}]
});

function readNetwork() {

var store = new Ext.data.Store({
model : 'Node',
proxy : {
type : 'ajax',
url : this.settings.get('url') + 'network.xml',
reader : {
type : 'xml',
root : 'node',
idProperty : 'nodeID'
}
},
listeners : {
single : true,
datachanged : function() {
Ext.getBody().unmask();
store.each(function(rec) {
network.push({
progID : rec.get('progID'),
nodeID : rec.get('nodeID'),
deviceClass : rec.get('deviceClass'),
nodeName : rec.get('nodeName')
});
});
}
}
});

store.read();
}
I couldn't find any information on how to define models that contain associations nor on how to read the nested XML. Can anybody help?

Many thanks in advance

Carsten

kortovos
7 Sep 2010, 2:12 AM
I have been struggeling with the same problem, but the solution is quit simple
Ext.regModel('Node', {
fields : [{
name : 'progID',
type : 'string'
}, {
name : 'nodeID',
type : 'string'
}, {
name : 'deviceClass',
type : 'string'
}, {
name : 'nodeName',
type : 'string'
}, {
name : 'varName',
mapping : 'variable > varName',
type : 'string'
}]
});

You can get the varName with
rec.get('varName')

This offcourse will only work when there is only one 'VarName' and not more. I am currently working on a method to solve that nuisance.