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) {
新聞熱點
疑難解答
圖片精選