PDA

View Full Version : Read data from XML source.



peterkuli
5 Dec 2010, 6:06 AM
I want to read data from a XML source and show it in a list.

With help from the examples of the XMLReader (http://dev.sencha.com/deploy/touch/docs/?class=Ext.data.XmlReader) class, I made this simple code:



Ext.setup({
onReady: function() {
Ext.regModel('Blog', {
fields: ['name', 'email']
});

var store = new Ext.data.Store({
model: 'Blog',
proxy: {
type: 'ajax',
url : 'test.xml',
reader: {
type: 'xml',
root: 'users',
record: 'user'
}
}
});

var list = new Ext.List({
fullscreen: true,

itemTpl : '{name} {email}',
grouped : false,
indexBar: false,

store: store
});
list.show();
}
});

The XML is also an example:


<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<id>1</id>
<name>Ed Spencer</name>
<email>ed@sencha.com</email>
</user>
<user>
<id>2</id>
<name>Abe Elias</name>
<email>abe@sencha.com</email>
</user>
</users>


The list is shown because when I scroll there's a scrollbar shown, but there's no data in it. I don't see any errors in the console (Chrome). When I put the URL of the XML directly in the addressbar, Chrome shows the XML.

I don't know why there's no data in the list. Anyone an idea?

evant
5 Dec 2010, 2:16 PM
You never tell the store to load.

peterkuli
6 Dec 2010, 1:25 AM
Ok...I put store.load(); just before I declare list.

I can now see that test.xml is actually loaded by the browser (Resources in Chrome) and the content-type is application/xml.

But, still not loaded in my list.

Sandpack.Ni
6 Dec 2010, 10:01 PM
proxy: {
type: 'ajax',
url : 'test.xml',
reader: {
type: 'xml',
root: 'users', -------Delete this code
record: 'user'
}
}

Then have a try:)

peterkuli
7 Dec 2010, 12:14 AM
Thanks! Works like a charm!

But I don't understand it completely because if you look at the examples in the API, a root configuration is used with exactly the same XML. That's mainly why I used a root configuration in the first place.

Sandpack.Ni
7 Dec 2010, 12:54 AM
Sorry. I don't understand either. I just followed the Sencha Touch Example (Carousel2) which is now with bug in it since updated to 1.0.1.
http://dev.sencha.com/deploy/touch/examples/carousel2/

You can download Sencha Touch 1.0RC to check it.
http://www.sencha.com/products/touch/thank-you.php?dl=touch1

peterkuli
7 Dec 2010, 2:28 AM
Yes, I see...it works for now. Thanks for the tip!