问题
Im using a nested XML and parsing it using 'hasMany'.. I would appreciate if someone could tell me how to read the value of the node '<type>'. I can easily read the attributes 'id' & 'val' of '' using mapping but i want to also read the node value eg. 257411 in <type id="3" val="0">257411 I would appreciate if anyone could provide a suitable 'mapping'
XML data:
<?xml version="1.0" encoding="ISO-8859-1"?>
<basics id="744" name="social">
<number>302221</number>
<types>
<type id="3" val="0">257411</type>
<type id="2" val="1081337">28213</type>
<type id="1" val="263258">8645</type>
<type id="5" val="0">3664</type>
<type id="4" val="0">2246</type>
<type id="9" val="0">1124</type>
<type id="10" val="0">918</type>
</types>
</basics>
model Basic Ext.define("ap3.model.Basic",{ extend: "Ext.data.Model",
config: {
fields: [
{name: 'id', mapping: '@id'},
{name: 'name', mapping: '@name'},
{name: 'number', mapping: 'number'}
],
associations: [
{
type: 'hasMany',
model: 'apv3.model.Type',
associationKey: 'types'
}]
}
});
model Type Ext.define("ap3.model.Type",{ extend: "Ext.data.Model",
config: {
fields: [
{name: 'id', mapping: '@id'},
{name: 'val', mapping: '@val'},
{name: 'type', mapping: 'type'}
],
proxy: {
type: 'memory',
reader: {
type: 'xml',
record: 'type'
}
}
}
});
回答1:
"mapping" accepts a function too:
{name: 'id', mapping: '@id'},
{name: 'name', mapping: '@name'},
{name: 'number', mapping: function (node) {
return (node.firstChild ? node.firstChild.nodeValue : null);
}}
回答2:
Don't know if that counts as an answer, but having spent a lot of time on the same issue I'd say this is a bug in the framework :(
A couple of relevant links: https://stackoverflow.com/a/11549596/454266 http://www.sencha.com/forum/showthread.php?209025-Nested-XML-reader-problem&p=838952&viewfull=1#post838952
Luckily nested JSON works fine. My advice is to switch to it or to handle loading of the lower-level models manually (e.g. writing your own method doing the job of hasMany association by creating a store / reader and feeding this.raw.types to it).
来源:https://stackoverflow.com/questions/12176719/sencha-touch-2-nested-xml-parsing-nodevalue