PDA

View Full Version : Problem with XML Reader



humbert
22 Dec 2010, 7:25 AM
Hello everyone. I'm new on JavaScript coding, and I´m having a trouble with the XMLReader. Maybe I'm doing somethig wrong, but i'm trying to retrieve data from an XML file. I readed in som thread that i have to use a web server to make this work, so i installed Xampp in my PC and moved all the sencha files into it. I started this aplication using the ajax example, to know what sources to include into it. Here's the code, it's very simple but it isn't working:



<html>
<head>
<content-type: "text/xml">
<link rel="stylesheet" href="../../resources/css/sencha-touch.css" type="text/css">
<script type="text/javascript" src="../../sencha-touch.js"></script>
<script type="text/javascript" src="index.js"></script>
<script type="text/javascript">
//I create the model
Ext.regModel('User', {
fields: ['id', 'name', 'email']
});
//I start a var using the model structure
var user = Ext.ModelMgr.create(
{
id: '4',
name: 'Conan',
email: 'conan@hotmail.com',
},
'User');
//I create the Store using the Model
var store = new Ext.data.Store(
{
model: 'User',
// I create the Store's proxy
proxy: {
type: 'ajax',
url : 'http://localhost/sencha/examples/ajax/users.xml',
// I create the xml reader to consume the xml file.
reader: {
type: 'xml',
root: 'users',
record: 'user',
}
}
});
// I call the method load to fill the store with the xml data.
store.load();
// Just to test the store I add the User var into it
store.insert(4,'User');
// I write the quantity of records in the store.
document.write(store.data.getCount());
// This always returns just one, the one i added with the insert() method.
</script>
<title>Prueba XML</title>
</head>
<body>
</body>
</html>


And this is the xml file users.xml:



<?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>


Please, can anyone tell me what's wrong?? :((:((:((

Thanks.-

Humberto Araoz.-

doncoleman
22 Dec 2010, 9:17 PM
Try this:

index.html


<html>
<head>
<link rel="stylesheet" href="sencha-touch.css" type="text/css">
<script type="text/javascript" src="sencha-touch-debug.js"></script>
<script type="text/javascript" src="app.js"></script>
<title>Prueba XML</title>
</head>
<body>
</body>
</html>


app.js


/*global Ext*/
Ext.setup({
onReady: function() {

Ext.regModel('User', {
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'email', type: 'string'}
]
});

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

store.load();

var panel = new Ext.Panel({
fullscreen: true,
items: [
{
xtype: 'list',
store: store,
itemTpl: "\"{name}\" &lt;{email}&gt;",
singleSelect: true
}
]
});
}
});

gaurav.k
10 Nov 2011, 9:20 PM
@doncoleman (http://www.sencha.com/forum/member.php?195382-doncoleman)
I have posted my problem on 3 different threads, but I din't get any solution or suggestion for that.

I am facing the same problem and tried your suggestion but din't worked.
With all the efforts I make one thing is in common, the error!!
and thats :

XMLHttpRequest cannot load file://localhost/Users/gauravkeshre/Sites/Sancha-touch Workspace/ALS_SHG/app/xml/sample.xml?_dc=1320988507322&limit=25. Origin null is not allowed by Access-Control-Allow-Origin.

If your problem is solved. Please share the solutions.

Thank you