/**
* 加載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) {
return node ? node.lastChild : this.getRoot().lastChild;
},
/**
* 返回節點的下一個節點,沒有參數則返回根節點的第一個子節點
* @param {Object} node
* @return {Object} 節點的下一個節點
*/
getNextNode : function (node) {
return node ? node.nextSibling : null;
},
/**
* 返回節點的上一個節點,沒有參數則返回根節點的第一個子節點
* @param {Object} node
* @return {Object} 節點的上一個節點
*/
getPreviousNode : function (node) {
return node ? node.previousSibling : null;
},
/**
* 返回節點的子節點,沒有參數則返回null
* @param {Object} node
* @return {Object} 節點所有子節點
*/
getChildNodes : function (node) {
return (node && this.hasChildNodes(node)) ? node.childNodes : null;
},
/**
* 返回節點的父節點,沒有參數則返回null
* @param {Object} node
* @return {Object} 節點父節點
*/
getParentNode : function (node) {
return node ? node.parentNode : null;
},
/**
* 根據節點名返回節點數組文本值,參數:
* @param {string或object} nodeName:節點名稱;
* @return {object} 節點存在返回節點數組;節點不存在則返回null。
*/
getNodesTextByName : function (nodeNames) {
return nodeNames ? (this.dataType == 'json' ? this.getJsonNodesTextByName(nodeNames) : this.getArryNodesTextByName(nodeNames)) : null;
},
/**
* 根據節點名返回節點普通數組文本值,參數:
* @param {string或object} nodeName:節點名稱;
* @return {object} 節點存在返回節點普通數組。
*/
getArryNodesTextByName : function (nodeNames) {
var rs = [];
//返回普通數組格式
switch (typeof(nodeNames)) {
case 'string':
var nodes = this.getNodesByTagName(nodeNames);
for (var i = 0; i < nodes.length; i++) {
rs.push(nodes[i].text);
}
break;
case 'object':
var subRs;
var nodes;
for (var i = 0; i < nodeNames.length; i++) {
nodes = this.getNodesByTagName(nodeNames[i]);
subRs = [];
for (var j = 0; j < nodes.length; j++) {
subRs.push(nodes[j].text);
}
rs.push(subRs);
}
break;
}
return rs;
},
/**
* 根據節點名返回節點JSON數組文本值,參數:
* @param {string或object} nodeName:節點名稱;
* @return {object} 節點存在返回節點JSON數組;節點不存在則返回null。
*/
getJsonNodesTextByName : function (nodeNames) {
var rs = null;
//返回JSON數組格式
switch (typeof(nodeNames)) {
case 'string':
eval('rs = {' + nodeNames + ':[]}');
var nodes = this.getNodesByTagName(nodeNames);
for (var i = 0; i < nodes.length; i++) {
eval('rs.' + nodeNames + '.push({' + nodeNames + i + ': nodes[i].text})');
}
break;
case 'object':
rs = {};
var nodes;
for (var i = 0; i < nodeNames.length; i++) {
eval('rs.' + nodeNames[i] + '=[]');
nodes = this.getNodesByTagName(nodeNames[i]);
for (var j = 0; j < nodes.length; j++) {
eval('rs.' + nodeNames[i] + '.push({' + nodeNames[i] + j + ': nodes[j].text})');
}
}
break;
}
return rs;
},
/**
* 根據節點屬性得到節點,參數:
* @param {String} key:屬性名,默認是id
* @param {String} value:屬性值
* @return {String} 符合條件的節點數組。
*/
getNodesByAttribute : function (key, value) {
key = key ? key : 'id';
value = value ? value : '';
return id ? this.xmlDoc.getElementById(id) : null;
},
/**
* 根據節點名得到節點,參數:
* @param {string} tagName:節點名稱
* @return {string} 指定節點名字的和位置的節點或節點數組。
*/
getNodesByTagName : function (tagName) {
return tagName ? this.xmlDoc.getElementsByTagName(tagName) : null;
},
/** /** /** /** /** return node ? node.appendChild(childNode) : childNode; /** /** /** /** /** /** /** /** /** /** 聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
* 根據節點路徑返回第index個節點,參數:
* @param {string} xPath:節點路徑
* @param {number}index:要索引的位置,為空或0則返回所有查找到的節點。
* @return {string} 指定節點名字的和位置的節點或節點數組。
*/
getNodesByXpath : function (xPath, index) {
if (!xPath) return null;
var nodes = this.xmlDoc.selectNodes(xPath);
var len = nodes.length;
if(!index || index > len || index < 0) return nodes;
for(var i=0; i
}
},
* 得到指定節點文本,參數:
* @param {object} node:節點
* @return {string} 節點文本,為空則返回null
*/
getText : function (node) {
return node ? node.text : null;
},
* 得到指定節點名稱,參數:
* @param {object} node:節點
* @return {string} 節點名稱,為空則返回null
*/
getTagName : function (node) {
return node ? node.nodeName : null;
},
* 返回節點類型,參數:
* @param {object} node:節點
* @return {string} 節點類型,為空則返回null
* 1-element
* 2-attribute
* 3-text
* 4-cdata
* 5-entity reference
* 6-entity
* 7-pi (processing instruction)
* 8-comment
* 9-document
* 10-document type
* 11-document fragment
* 12-notation
*/
getNodeType : function (node) {
return node ? node.nodeType : null;
},
* 創建節點,參數:
* @param {string} nodeName:節點名稱,必填
* @param {string} text:節點文本,可為空
* @param {Object} attributes:屬性值-JSON數組,可為空,例:{id:'id001',name:'name001'}
* @param {Object} node:要增加子節點的節點,為空則返回新建的節點
* @param {Boolean} cdata:是否生成帶有CDATA區段的節點,true:生成,false:不生成
* @return {Object} 創建的節點,有異常則返回null
*/
createNode: function(nodeName, text, attributes, node, cdata) {
if (this.isIE) {
//創建子接點
var childNode = this.xmlDoc.createElement(nodeName);
//創建文本節點
var textNode = cdata == true ? this.xmlDoc.createCDATASection(text) : this.xmlDoc.createTextNode(text);
childNode.appendChild(textNode);
//添加屬性
for (var i in attributes) {
this.createAttribute(childNode,i,attributes[i]);
};
} else {
alert('FF創建節點再說.');
return null;
}
},
* 創建帶CDATA區段的節點,參數:
* @param {string} nodeName:節點名稱,必填
* @param {string} text:節點文本,可為空
* @param {Object} attributes:屬性值-JSON數組,可為空,例:{id:'id001',name:'name001'}
* @param {Object} node:要增加子節點的節點,為空則返回新建的節點
*/
createCDATANode: function(nodeName, text, attributes, node) {
this.createNode(nodeName, text, attributes, node, true);
},
* 創建節點屬性,參數:
* @param {Object} node:節點,必填
* @param {String} key:屬性名,必填
* @param {Object} value:屬性值,必填
* @param {Object} node:返回新增屬性的節點
* @return {Object} 增加屬性的節點,有異常則返回null
*/
createAttribute: function(node, key, value) {
if (this.isIE) {
if(!key) return;
var attr = this.xmlDoc.createAttribute(key);
attr.value = value ? value : "";
node.setAttributeNode(attr);
return node;
} else {
alert('FF創建節點再說.');
return node;
}
return null;
},
* 把節點加到根節點上,參數:
* @param {Object} node:節點
* @return {Object} 有異常則返回null
*/
addNodeToRoot: function(node) {
if(!node) return null;
this.getRoot().appendChild(node);
return node;
},
* 把節點加到另外節點上,參數:
* @param {Object} node:節點
*/
addNode: function(node,childNode) {
return (node && childNode) ? node.appendChild(childNode) : false;
},
* 從父節點移除節點自身,參數:
* @param {Object} newNode:要替換的節點
* @param {Object} oldNode:要被替換的節點
*/
replaceChild: function(newNode, oldNode) {
var parentNode = oldNode.parentNode;
if(!newNode || !oldNode || !parentNode) return;
parentNode.replaceChild(newNode, oldNode);
},
* 從父節點移除節點自身,參數:
* @param {Object} node:要移除的節點
*/
removeChild: function(node) {
if(!node || !node.parentNode) return;
node.parentNode.removeChild(node);
},
* 移除節點的所有子節點,參數:
* @param {Object} node:父節點
*/
removeChildNodes: function(node) {
if (node && this.hasChildNodes(node)) {
var childNodes = node.childNodes;
for(var i = 0; i < childNodes.length; i++) {
node.removeChild(childNodes[0]);
}
}
},
* 設置節點屬性值,不存在則新建,參數:
* @param {Object} node:要設置的節點
* @param {String} key:要設置的屬性名
* @param {String} value:要設置的屬性值
*/
setAttribute: function(node, key, value) {
this.createAttribute(node, key, value);
},
* 設置文本節點的文本,參數:
* @param {Object} node:要設置的節點
* @param {String} text:要設置的文本
*/
setText: function(node, text) {
if(this.isTextNode(node)) node.text = text;
},
* 在文本節點后面追加文本,參數:
* @param {Object} node:要設置的節點
* @param {String} text:要設置的文本
*/
appendText: function(node, text) {
if(this.isTextNode(node)) node.appendData(text);
},
/**
* 輸出xml,為空則輸出根節點文本,參數:
* @param {Object} node:要輸出的節點
*/
toString: function(node) {
node = node ? node : this.xmlDoc.documentElement;
if (typeof node == 'string') return node;
return this.isIE ? node.xml : new XMLSerializer().serializeToString(node);
}
}
測試的xml文件(book.xml):
代碼如下:
吳承恩
曹雪芹
羅貫中
html code (test.html):
代碼如下: