demo用了点extjs的东西,主要是为了打印json数组出来。
js code(XmlUtils.js):
代码如下:
/**/
function XmlUtils (config) {
/*定义私有属性*/
this.isIE = !!(window.attachEvent && !window.opera);
this.init();
if(config) {
this.dataType = config.dataType == 'json' ? 'json' : 'array';
if(config.xmlPath) this.loadXml(config.xmlPath);
}
}
XmlUtils.prototype = {
xmlDoc : null,
xmlPath : null,
dataType : null,
/**
* 初始化
*/
init : function () {
if (this.isIE) {
var activexArr = ["MSXML4.DOMDocument", "MSXML3.DOMDocument", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XmlDom"];
for(i=0; i<activexArr.length; i++){
try{
this.xmlDoc = new ActiveXObject(activexArr[i]);
}catch(e){}
}
} else {
this.xmlDoc = document.implementation.createDocument("", "", null);
}
},
/**
* 加载xml文件,参数:
* @param {string} xmlPath:加载的xml文件路径;
* @return {Object} true 正常加载; false 加载失败
*/
loadXml : function (xmlPath) {
try {
this.xmlDoc.async = false;
this.xmlDoc.load(xmlPath);
this.xmlPath = xmlPath;
return true;
} catch (e) {
return false;
}
},
/**
* 加载XML字符串
* @param {Object} XMLString
*/
loadXmlString: function(xmlString) {
if (this.isIE) {
this.xmlDoc.loadXML(xmlString);
} else {
var parser = new DOMParser();
this.XMLDoc = parser.parseFromString(xmlString, "text/xml");
}
},
/**
* 判断节点的是否有子节点
* @param {Object} node
* @return {Object} 有子节点则返回true,否则返回false
*/
hasChildNodes : function (node) {
return node.hasChildNodes();
},
/**
* 判断节点的是否有属性
* @param {Object} node
* @return {Object} 有属性则返回true,否则返回false
*/
hasAttributes : function (node) {
return (node.attributes.length > 0) ? true : false;
},
/**
* 判断节点的是否是文本节点,包括带CDATA区段的文本节点
* @param {Object} node
* @return {Object} 是文本节点则返回true,否则返回false
*/
isTextNode : function (node) {
var type = this.getNodeType(node);
return (type == 3 || type == 4) ? true : false;
},
/**
* 返回根节点
* @return {Object} 根节点
*/
getRoot : function () {
return this.xmlDoc.documentElement;
},
/**
* 返回节点的第一个子节点,没有参数则返回根节点的第一个子节点
* @param {Object} node
* @return {Object} 节点的第一个子节点
*/
getFirstChild : function (node) {
return node ? node.firstChild : this.getRoot().firstChild;
},
/**
* 返回节点的最后子节点,没有参数则返回根节点的第一个子节点
* @param {Object} node
* @return {Object} 节点的最后一个子节点
*/
getLastChild : function (node) {
新闻热点
疑难解答
图片精选