PDA

View Full Version : XML mapping for attribute always returns empty



keckeroo
27 Mar 2012, 4:15 PM
I'm stuck - really ;-)

So xml is as follows :



<eparcel>
<ratesAndServicesResponse>
<statusCode>1</statusCode>
<statusMessage>OK</statusMessage>
<requestID>3555795</requestID>
<handling>1.0</handling>
<language>0</language>
<product id="1040" sequence="1">
<name>Priority Courier</name>
<rate>34.08</rate>
<shippingDate>2012-03-28</shippingDate>
<deliveryDate>2012-03-29</deliveryDate>
<deliveryDayOfWeek>5</deliveryDayOfWeek>
<nextDayAM>true</nextDayAM>
<packingID>P_0</packingID>
</product>
<product id="1030" sequence="2">
<name>Xpresspost</name>
<rate>19.25</rate>
<shippingDate>2012-03-28</shippingDate>
<deliveryDate>2012-03-30</deliveryDate>
<deliveryDayOfWeek>6</deliveryDayOfWeek>
<nextDayAM>false</nextDayAM>
<packingID>P_0</packingID>
</product>
<product id="1020" sequence="3">
<name>Expedited</name>
<rate>11.46</rate>
<shippingDate>2012-03-28</shippingDate>
<deliveryDate>2012-04-02</deliveryDate>
<deliveryDayOfWeek>2</deliveryDayOfWeek>
<nextDayAM>false</nextDayAM>
<packingID>P_0</packingID>
</product>
<shippingOptions>
<insurance>No</insurance>
<deliveryConfirmation>Yes</deliveryConfirmation>
<signature>No</signature>
</shippingOptions>
</ratesAndServicesResponse>
</eparcel>


and model is defined like this :



Ext.define('MyStore.model.ShippingOption', {
extend: 'Ext.data.Model',

fields: [
'name', 'rate', 'shippingDate',
{ name: 'shipcode', mapping: 'product/@id' }
]
});


and store is :



Ext.define('MyStore.store.ShippingOptions', {
extend: 'Ext.data.Store',

model: 'MyStore.model.ShippingOption',
autoLoad: false,

proxy: {
type: 'ajax',
url: '/api/CanadaPostRates.php',
reader: {
type: 'xml',
root: 'eparcel',
record: 'product'
}
}
});



I can get all my record info perfectly into the store - however attribute 'id' for <product id=??>
is blank when I reference record.data.shipcode. All other info is fine though.

Any ideas ?

Thanks!

Kev

keckeroo
28 Mar 2012, 8:18 AM
Found the answer - apparently you can map using the following to access the attribute in the record node itself.




..
{ name: 'shipcode', mapping: '@id' }
...



PS - this unfortunately doesn't help in obtaining attributes embedded in child nodes of the record - only the root node. Not sure how I could do that.

Kev