PDA

View Full Version : Nested XML reader problem



newuser7000
25 May 2012, 5:42 PM
Hi there,
I am learning ExtJS from examples for a few days. Following the examples were easy and straight-forward until I reached to read Nested XML file. I felt stuck... I think there are potential bugs when reading nested Xml with associations. (e.g.follow the example from http://docs.sencha.com/ext-js/4-0/#/guide/data).


--> I just want to show/log all comments, like this:


User: user name
Comments for post: post title 1
Message: comment 1
Message: comment 2
Comments for post: post title 2
Message: comment 3
Message: comment 4
Endereco is: 1230
----
:((I think ExtJS 4.1 seems to have problems to load nested Xml data with associations, with Json format everything seems to be OK.



<?xml version="1.0" encoding="utf-8"?>
<xml>
<users>
<id>2</id>
<name>user name</name>
<age>25</age>
<gender>male</gender>
<posts>
<id>1</id>
<title>post title 1</title>
<body>post body 1</body>
<comments>
<id>1</id>
<name>comment name 1</name>
<message>comment 1</message>
</comments>
<comments>
<id>2</id>
<name>comment name 2</name>
<message>comment 2</message>
</comments>
</posts>
<posts>
<id>2</id>
<title>post title 2</title>
<body>post body 2</body>
<comments>
<id>3</id>
<name>comment name 3</name>
<message>comment 3</message>
</comments>
<comments>
<id>4</id>
<name>comment name 4</name>
<message>comment 4</message>
</comments>
</posts>
<endereco>
<id>1</id>
<logradouro>rua ficticia</logradouro>
<numero>1230</numero>
<complemento>apt 15</complemento>
</endereco>
</users>
</xml>

mitchellsimoens
7 Jun 2012, 11:53 AM
What model definitions have you tried?

newuser7000
8 Jun 2012, 1:00 PM
Ext.require('Ext.data.Store');
Ext.require('Ext.data.reader.Xml');

Ext.define('User',
{
extend: 'Ext.data.Model',
fields: ['id', 'name'],
proxy:
{
type: 'ajax',
url : 'users.adaption.xml',
reader:
{
type: 'xml',
record: 'users'
}
},
associations : [
{type: 'hasMany', name: 'posts', model: 'Post'},
{type: 'hasOne', name: 'endereco', model: 'Endereco'}
]
});

Ext.define('Post',
{
extend: 'Ext.data.Model',
fields: ['id', 'user_id', 'title', 'body'],
proxy:
{
type: 'ajax',
reader:
{
type: 'xml',
record: 'posts'
}
},
hasMany:
{
name: 'comments',
model: 'Comment'
},
belongsTo: 'User'
});

Ext.define('Comment',
{
extend: 'Ext.data.Model',
fields: ['id', 'post_id', 'name', 'message'],
proxy:
{
type: 'ajax',
reader:
{
type: 'xml',
record: 'comments'
}
},
belongsTo: 'Post'
});


Ext.define('Endereco',
{
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'logradouro', type: 'string'},
{name: 'numero', type: 'string'},
{name: 'complemento', type: 'string'},
{name: 'pessoaId', type: 'int'},
{name: 'user_id'}
],
proxy:
{
type: 'ajax',
url : 'users.adaption.xml',
reader:
{
type: 'xml',
record: 'endereco'
}
}
});


Ext.onReady(function()
{
User.load(1,
{
success: function(user)
{
console.log('User: ', user.get('name'));

user.posts().each(function(post)
{
console.log(' Comments for post: ', post.get('title'));

post.comments().each(function(comment)
{
console.log(' Message: ', comment.get('message'));
});

// var end = user.getEndereco();
// console.log(end.get('logradouro') + ' ' + end.get('numero'));
// console.log('Endereco e: ' + user.getEndereco().get('numero'));

});
}

});
})

sapkalparag
14 Jun 2012, 11:23 PM
Hi newuser7000

Even I am trying the same. I am doing it in Sencha touch 2, though. But that won't be much of a difference though. Did you find a way to do it? to access the 'comments' part?

newuser7000
21 Jun 2012, 10:54 AM
Hi there,
I don't know about sencha touch, but there is an workaround in ExtJS.
I think sencha is taking JSON over from XML as the preferred way to manipulate metadata. I have been noticing that persist "I think a bug" from 4.0 - 4.1 ExtJS version - related to nested XML and associations. I think data package library is shared in both touch and extjs.
In ExtJS 4, I have modified 4 files, and now is working fine!
extjs/src/data/reader/Reader.js
extjs/src/data/reader/Xml.js
extjs/src/core/src/dom/DomQuery.js
extjs/ext-all-debug.js

I think sencha team didn't fix this yet because the problem is bigger than a pineapple.

regards,
warleyalex

snehalv
28 Aug 2012, 4:39 AM
This problem can be solved if you specify the reader at the association level like:


Ext.define('User', { extend: 'Ext.data.Model', fields: ['id', 'name'], proxy: { type: 'ajax', url : 'users.adaption.xml', reader: { type: 'xml', record: 'users' } }, associations : [ {type: 'hasMany',
name: 'posts',
model: 'Post',
associationKey:'posts',
reader : {
type : 'xml',
record : 'posts'
}

} ] });

SpaceNinjaDino
19 Sep 2012, 11:56 AM
I'm having a problem with simply the xml-form.html example. When I click load, it calls the failure function. It works from the dev.sencha.com website, but not on my local computer.

I do not understand what's going on because the xml-grid.html example works fine. I downloaded both 4.1.1 and 4.0.7 and get the same result. I'm not running an official web server (just from file explorer and Aptana) which makes me worried that somehow these XML readers need php or something more than HTTP get/post. Our project requires that our web server only does get/post and the data is in XML format.

However, since ext-all.js is just JavaScript, I do not understand why there are problems. The browser is fine since remote examples are working.

---- Update: I see the message when I download ExtJS: Any samples that use Ajax will need to be on an web server in order to work correctly.
What are the minimum requirements of the web server? Does it need to be able to run PHP, JVM or something else behind the scenes? I've asked around before and got answers that Ajax only needs HTTP get/post. Not sure how Aptana/file explorer can't handle that. Sorry for such a newbie question. I'm transitioning from Java Swing dev.

----- Update 2: I downloaded MiniShare which is a super light web server for Windows. The XML Form example fails there too. But the XML Grid still succeeds. I look through ExtJS's library and only see JS files. The only PHP files are for some examples.

Digging some more, I see that XMLHttpRequest is a big part of Ajax. Does it take a more sophisticated web server to handle that? I still don't understand why the XML Grid can work (it declares 'ajax' in the example), but not XML Form.

---- Update 3: I installed WAMP and now have things working. I am still concerned that MiniShare did not work. It seems the whole XMLHttpRequest requires that the file is coming from the same server. Hopefully our official web server will be better than MiniShare's capabilities.