PDA

View Full Version : XML Parser Problem



MitchellNZ
10 Nov 2014, 2:21 PM
Hi guys,

I am currently having a problem to do with XML parsing when I have nested child nodes.

In my example below, I show the XML I am getting where a User has a 'type' attribute and Tool has 'type' attribute. (The users 'type' is just a string, but the tools 'type' is an object, I'm not sure if this is related to the problem).

My problem is that the XML parser seems to be picking up both 'type' attributes when mapping my User object and then getting confused, resulting in the User's 'type' attribute being 'null' (where I am expecting it to be the string 'Normal').

Code examples:

XML example:

<user>
<id>1</id>
<userName>John Smith</userName>
<type>Normal</type>
<tools>
<tool>
<id>100</id>
<name>Drill</name>
<type>
<size>5.3</size>
<model>Brand name</model>
</type>
</tool>
</tools>
</user>


Store example:

Ext.create('Ext.data.Store', {
autoLoad: true,
model: 'User',
data: xmlData,
proxy: {
type: 'memory',
reader: {
type: 'xml',
record: 'user'
}
}
});


Model example:

Ext.define('User', {
extend: 'Object',
fields: [
{name: 'id'},
{name: 'userName'}
{name: 'type'}
],
hasMany: {
model: 'Tool',
name: 'tools',
reader: {
type: 'xml',
record: 'tool'
}
});


Ext.define('Tool', {
extend: 'Object',
fields: [
{name: 'id'},
{name: 'name'}
]
});


Any help would be much appreciated.

Thanks!

MitchellNZ
11 Nov 2014, 12:42 PM
After much digging around trying different possible solutions..

I finally found a simple way to achieve my goal!

For anyone else having this same problem, I managed to work around this by adding a 'mapping' to my top level class's 'type' field that just appended a '>' in front of the property name.

{name: 'type', mapping: '>type'}

Example:

Ext.define('User', {
extend: 'Object',
fields: [
{name: 'id'},
{name: 'userName'}
{name: 'type', mapping: '>type'}
],
hasMany: {
model: 'Tool',
name: 'tools',
reader: {
type: 'xml',
record: 'tool'
}
});


Hope this helps someone else too!

Thanks.