日期:2014-05-16 浏览次数:20475 次
由于经常接触WRT,所以写了个一个解析XML和JSON的代码,比较通用。
解析XML的是很,代码会根据相关的配置,将节点组合成对象,然后返回。
?
请看代码:
NetWorkUtils.js - 用于Ajax网络连接
?
?
/**
* 抓取网络数据,并进行处理
* @param url 数据的url
* @param param 需要传递的参数
* @param callback 处理数据的函数
* @param exceptionCallBack 出现异常如何处理的函数
* @param isXML 是否是xml格式的数据,true 或者是false
* @return
*/
function ajaxFetch(url, param, callback,exceptionCallBack, isXML) {
var req = new Ajax();
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200) {
if (!isXML && req.responseText != null) {
callback(req.responseText);
} else if (isXML && req.responseXML != null) {
callback(req.responseXML);
} else {
callback(null);
}
} else if (req.readyState == 4 && req.status != 200) {
if(exceptionCallBack!=null&&exceptionCallBack!=undefined)
exceptionCallBack();
}
}
var fullURL = url;
if (fullURL.indexOf("?") == -1) {
fullURL += "?";
} else {
fullURL += "&";
}
fullURL += "timestamp=" + (new Date().getTime());
fullURL += "&" + param;
req.open("GET", fullURL, true);
req.send(null);
}
?
?
?
ProcessData.js - 用于处理数据并且进行回调
?
?
/**
* 构造方法
* @param {Object} Url xml JSON地址
* @param {Object} parentNode 父节点,比如要把<item><title>aaa</title></item>转成对象,此参数就是item
* @param {Object} options
* @param {Object} processDataFunction 數據對象轉換完成后需要調用的方法
*/
function ProcessData(Url, parentNode, options, processDataFunction){
this.URL = xmlUrl;
this.parentNode = parentNode;
this.options = options;
this.processDataFunction = processDataFunction;
selfProcessData = this;
}
ProcessData.prototype = new Object();
ProcessData.prototype.URL;
ProcessData.prototype.options;
ProcessData.prototype.parentNode;
ProcessData.prototype.processDataFunction;
ProcessData.prototype.dataArray;
/**
* 处理XML数据调用的方法
* @param {Object} exceptionFunction 抓取数据时候发生异常需要做的事情,比如提示重新连接等等
*/
ProcessData.prototype.buildxml = function(exceptionFunction){
ajaxFetch(selfProcessData.URL, null, selfProcessData.buildXMLCallBack, exceptionFunction, true);
}
/**
* 处理JSON数据调用的方法
* @param {Object} exceptionFunction 抓取数据时候发生异常需要做的事情,比如提示重新连接等等
*/
ProcessData.prototype.buildJSON = function(exceptionFunction){
ajaxFetch(selfProcessData.URL, null, selfProcessData.buildJSONCallBack, exceptionFunction, false);
}
/**
* 抓取JSON后回调方法
* @param {string} JSON字符串
*/
ProcessData.prototype.buildJSONCallBack = function(data){
if (data == null && data == undefined) {
return null;
}
json = stringToJSON(data);
selfProcessData.processDataFunction(json);
}
/**
* 抓取xml后回调方法
* @param {Object} data dom 文档
*/
ProcessData.prototype.buildXMLCallBack = function(data){
if (data == null && data == undefined) {
return null;
}
var items = data.documentElement.getElementsByTagName(selfProcessData.parentNode);
if (items == null && items == undefined) {
return null;
}
var dataLength = items.length;
var array = [];
for (var i = 0; i < dataLength; i++) {
//Set property and data into object
selfProcessData.data1 = new Data();
var optionsLength = selfProcessData.options.length;
for (var j = 0; j < optionsLength; j++) {
var option = selfProcessData.options[j];
//要从XML读取的节点名称
var nodeName = option['nodeName'];
//取得节点元素
var da = items[i].getElementsByTagName(nodeName)[0];
//通过options取得对应的成员变量名称
var memberName = option['memberName'];
var nodeValueOrAttribute = option['nodeValueOrAttribute'];
var attributeName = option['attributeName'];
if (da != null || da != undefined) {
//判断是取节