今天无意中在Ext的论坛里面发现了一个和我同样问题的帖子,最后他解决了问题,我也顺便解决了.但是他没有把他的代码粘贴出来,所以我现在把我的代码粘贴出来供参考.
这是Xml文档
<?xml version="1.0" encoding="utf-8" ?>
<Items>
<item input="Connect.Host" value="value.Connect.Host" />
<item input="Connect.DB" value="value.Connect.DB" />
<item input="Connect.User" value="value.Connect.User" />
<item input="Connect.Password" value="value.Connect.Password" />
</Items>
<Items>
<item input="Connect.Host" value="value.Connect.Host" />
<item input="Connect.DB" value="value.Connect.DB" />
<item input="Connect.User" value="value.Connect.User" />
<item input="Connect.Password" value="value.Connect.Password" />
</Items>
下面是js代码:
var ds = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: 'test1.xml', method: 'GET'}),
reader: new Ext.data.XmlReader(
// records will have an "Item" tag
{record: 'item'},
[
// set up the fields mapping into the xml doc to extract *attributes*
{name: 'value', mapping: '@value'},
{name: 'input', mapping: '@input'}
])
});
ds.on('load',AJAX_Loaded, this, true);//这里需要注意
ds.load();
function AJAX_Loaded(){
alert(ds.getCount());
for (var i = 0; i < ds.getCount(); i++) {
var rec = ds.getAt(i);
alert("value = '" + rec.get("value"));
alert("input = '" + rec.get("input"));
}
}
proxy: new Ext.data.HttpProxy({url: 'test1.xml', method: 'GET'}),
reader: new Ext.data.XmlReader(
// records will have an "Item" tag
{record: 'item'},
[
// set up the fields mapping into the xml doc to extract *attributes*
{name: 'value', mapping: '@value'},
{name: 'input', mapping: '@input'}
])
});
ds.on('load',AJAX_Loaded, this, true);//这里需要注意
ds.load();
function AJAX_Loaded(){
alert(ds.getCount());
for (var i = 0; i < ds.getCount(); i++) {
var rec = ds.getAt(i);
alert("value = '" + rec.get("value"));
alert("input = '" + rec.get("input"));
}
}
需要注意的是,我们获得数据是通过load事件来实现的,这是很多人错误的地方,也就是说,如果我们不通过事件还是无法获得数据.