栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

XmlUtils JS操作XML工具类

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

XmlUtils JS操作XML工具类

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; itry{
this.xmlDoc = new ActiveXObject(activexArr[i]);
}catch(e){}
}
} else {
this.xmlDoc = document.implementation.createdocument("", "", null);
}
},


loadXml : function (xmlPath) {
try {
this.xmlDoc.async = false;
this.xmlDoc.load(xmlPath);
this.xmlPath = xmlPath;
return true;
} catch (e) {
return false;
}
},


loadXmlString: function(xmlString) {
if (this.isIE) {
this.xmlDoc.loadXML(xmlString);
} else {
var parser = new DOMParser();
this.XMLDoc = parser.parseFromString(xmlString, "text/xml");
}
},


hasChildNodes : function (node) {
return node.hasChildNodes();
},


hasAttributes : function (node) {
return (node.attributes.length > 0) ? true : false;
},


isTextNode : function (node) {
var type = this.getNodeType(node);
return (type == 3 || type == 4) ? true : false;
},


getRoot : function () {
return this.xmlDoc.documentElement;
},


getFirstChild : function (node) {
return node ? node.firstChild : this.getRoot().firstChild;
},


getLastChild : function (node) {
return node ? node.lastChild : this.getRoot().lastChild;
},


getNextNode : function (node) {
return node ? node.nextSibling : null;
},


getPreviousNode : function (node) {
return node ? node.previousSibling : null;
},


getChildNodes : function (node) {
return (node && this.hasChildNodes(node)) ? node.childNodes : null;
},


getParentNode : function (node) {
return node ? node.parentNode : null;
},


getNodesTextByName : function (nodeNames) {
return nodeNames ? (this.dataType == 'json' ? this.getJsonNodesTextByName(nodeNames) : this.getArryNodesTextByName(nodeNames)) : null;
},


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;
},


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;
},


getNodesByAttribute : function (key, value) {
key = key ? key : 'id';
value = value ? value : '';
return id ? this.xmlDoc.getElementById(id) : null;
},


getNodesByTagName : function (tagName) {
return tagName ? this.xmlDoc.getElementsByTagName(tagName) : null;
},


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; iif(i == index - 1) return nodes[i];
}
},


getText : function (node) {
return node ? node.text : null;
},


getTagName : function (node) {
return node ? node.nodeName : null;
},


getNodeType : function (node) {
return node ? node.nodeType : 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]);
};

return node ? node.appendChild(childNode) : childNode;
} else {
alert('FF创建节点再说.');
return null;
}
},


createCDATANode: function(nodeName, text, attributes, node) {
this.createNode(nodeName, text, attributes, node, true);
},


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;
},


addNodeToRoot: function(node) {
if(!node) return null;
this.getRoot().appendChild(node);
return node;
},


addNode: function(node,childNode) {
return (node && childNode) ? node.appendChild(childNode) : false;
},


replaceChild: function(newNode, oldNode) {
var parentNode = oldNode.parentNode;
if(!newNode || !oldNode || !parentNode) return;
parentNode.replaceChild(newNode, oldNode);
},


removeChild: function(node) {
if(!node || !node.parentNode) return;
node.parentNode.removeChild(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]);
}
}
},


setAttribute: function(node, key, value) {
this.createAttribute(node, key, value);
},


setText: function(node, text) {
if(this.isTextNode(node)) node.text = text;
},


appendText: function(node, text) {
if(this.isTextNode(node)) node.appendData(text);
},



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):
复制代码 代码如下:





测试xml








上面的文件都上传了,正在审核中,等审核通过了我会发到这里的。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/117683.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号