extjs之-读取xml

请求我们可以暂分为两种:一种是表单Form的请求提交,一种是AJAX的请求。
我们现在对这两种请求的返回结果进行读取。
在后台用我们自定义的XML格式。
然后写个JS对XML进行读取。

1:对AJAX请求返回的xml读取
var AjaxRead = {};
AjaxRead = function (doc){
this.xmlData = doc;
var root = doc.documentElement || doc;
var q = Ext.DomQuery;
var sv = q.selectValue("success", root, true);
success = sv !== false && sv !== 'false'&& sv!=='otherfalse';
var ns = q.select("field", root);
for(var i = 0, len = ns.length; i < len; i++) {
var n = ns[i];
var msg = q.selectValue("msg", n, 0);
var type = q.selectValue('@type',n,0);
if(type == 'success'){
Ext.MessageBox.show({
title:'成功',
msg: msg,
buttons: Ext.MessageBox.OK,
icon: Ext.MessageBox.INFO
})
}
else if(type=='exception'){
Ext.MessageBox.show({
title:'错误',
msg: msg,
buttons: Ext.MessageBox.OK,
icon: Ext.MessageBox.ERROR
})
}
}
}
2:对一般FORM请求返回的XML读取:
Ext.form.CMXmlErrorReader = function(){

//下面这段不理解,字面上是 传入当前对象给父类的构造方法
Ext.form.CMXmlErrorReader.superclass.constructor.call(this, {
totalRecords:"total",
record : 'field',
success: '@success'
}, [
'id', 'msg'
]
);
this.totalRecords=0;

}
//集成自XMLReader复写一下读数据时候的操作
Ext.extend(Ext.form.CMXmlErrorReader, Ext.data.XmlReader,
{
getTotalCount:function()
{
/*
var doc = this.xmlData;

if(doc==null)
{
return 0;
}
var root = doc.documentElement || doc;
var q = Ext.DomQuery;
var totalRecords = 0;
totalRecords = q.selectNumber(this.meta.totalRecords, root, 0);
//Ext.MessageBox.alert(totalRecords);
return totalRecords;
*/
return this.totalRecords;
}
,
readRecords : function(doc){
//this.meta指的是初始化xmlReader时的config配置项

this.xmlData = doc;
var root = doc.documentElement || doc;
var q = Ext.DomQuery;
var recordType = this.recordType, fields = recordType.prototype.fields;
var sid = this.meta.id;
var totalRecords = 0, success = true;

if(this.meta.totalRecords){
totalRecords = q.selectNumber(this.meta.totalRecords, root, 0);
this.totalRecords = totalRecords;
}
if(this.meta.success){
var sv = q.selectValue(this.meta.success, root, true);
success = sv !== false && sv !== 'false'&& sv!=='otherfalse';
}
var records = [];
var ns = q.select(this.meta.record, root);
for(var i = 0, len = ns.length; i < len; i++) {
var n = ns[i];
var values = {};
var id = sid ? q.selectValue(sid, n) : undefined;
// add by gjw
// var info = Ext.MessageBox.INFO;
var type = q.selectValue('@type',n,0);
var msg = q.selectValue('msg',n,0);
if(type == 'success'){
/*
Ext.MessageBox.show({
title:'成功',
msg: msg,
buttons: Ext.MessageBox.OK,
icon: Ext.MessageBox.INFO
})
*/
//Ext.Msg.alert('成功',v);
}
else if(type=='exception'){
// deal with common exception
Ext.MessageBox.show({
title:'错误',
msg: msg,
buttons: Ext.MessageBox.OK,
icon: Ext.MessageBox.ERROR
})
}
else if(type=='session_error'){
//deal with session error
RS.showLogin({msg:msg});//显示登录窗口页面
//console.log("session error:"+msg);
}
// end by gjw
for(var j = 0, jlen = fields.length; j < jlen; j++){
var f = fields.items[j];
var v = q.selectValue(f.mapping || f.name, n, f.defaultValue);
v = f.convert(v, n);
values[f.name] = v;
}
var record = new recordType(values, id);
record.node = n;
records[records.length] = record;
}

return {
success : success,
records : records,
totalRecords : totalRecords || records.length
};
}
}
);
xml格式如:
<message success="true"><field type="success"><msg><![CDATA[删除成功!]]></msg></field></message>
阅读更多

没有更多推荐了,返回首页