// JavaScript Document
/*JAVASCRIPT利用FSO对XML文件操作类
文件名:ClsReadXML.js
适用:windows系统下IE环境下适用
by adengou 2015.11
*/
//兼容IE9以下版本
if (!Object.create) {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
///
var _READXML ={};
_READXML.IsIE = function(){
判断浏览器类型及其版本
var userAgent = navigator.userAgent,
rMsie = /(msie\s|trident.*rv:)([\w.]+)/,
rFirefox = /(firefox)\/([\w.]+)/,
rOpera = /(opera).+version\/([\w.]+)/,
rChrome = /(chrome)\/([\w.]+)/,
rSafari = /version\/([\w.]+).*(safari)/;
var browser;
var version;
var ua = userAgent.toLowerCase();
var exploerAndVersion =function(ua) {
var match = rMsie.exec(ua);
if (match != null) {
return { browser : "IE", version : match[2] || "0" };
}
var match = rFirefox.exec(ua);
if (match != null) {
return { browser : match[1] || "", version : match[2] || "0" };
}
var match = rOpera.exec(ua);
if (match != null) {
return { browser : match[1] || "", version : match[2] || "0" };
}
var match = rChrome.exec(ua);
if (match != null) {
return { browser : match[1] || "", version : match[2] || "0" };
}
var match = rSafari.exec(ua);
if (match != null) {
return { browser : match[2] || "", version : match[1] || "0" };
}
if (match != null) {
return { browser : "", version : "0" };
}
}
var browserMatch = exploerAndVersion(userAgent.toLowerCase());
if (browserMatch.browser) {
browser = browserMatch.browser;
version = browserMatch.version;
}
return browser.indexOf("IE")>=0?true:false;
//alert(browser);
//document.write(browser+version);
/*EXAMPLE
if (browser.indexOf("IE")>=0 && parseInt(version) < 8){
$obj.prepend(iepanel);
$obj.prepend(overlay);
}
*/
}
///装载XML文件
_READXML.loadXmlFile = function(xmlFile){
var xmlDom = null;
if (window.ActiveXObject){
xmlDom = new ActiveXObject("Microsoft.XMLDOM");
xmlDom.async=false;
//isIE=true;
xmlDom.load(xmlFile)||xmlDom.loadXML(xmlFile);//如果用的是XML字符串//如果用的是xml文件
}else if (document.implementation && document.implementation.createDocument){
// isIE=false ;
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", xmlFile, false);
xmlhttp.send(null);
xmlDom = xmlhttp.responseXML;
}else{
// isIE=false;
xmlDom = null;
}
return xmlDom;
}
/
// 获取xmldoc
//_READXML.xmlDoc =_READXML.loadXmlFile(xmlFile);
//判断子节点为不为空
_READXML.NodeIsNull = function (xmlDoc,Nodename,index)
{
var obj =xmlDoc.getElementsByTagName(Nodename)[index];
return obj == null?true:false;
}
///获取节点的值
_READXML.GetNodeValue = function (xmlDoc,Nodename,index)
{
if(_READXML.NodeIsNull(xmlDoc,Nodename,index)){alert("error0007: 该节点不存在");return null;}
var nodevalue = "";
var obj =xmlDoc.getElementsByTagName(Nodename)[index];
if(obj!=null)
{
if(_READXML.IsIE()){
nodevalue =obj.childNodes[0].text;
}else{
nodevalue =obj.childNodes[0].nodeValue;
}
}
return nodevalue;
}
///
通过节点的关系节点名称树中导航。
// 这个方法是根据当前节点得到parentNode 、firstChild、lastChild、nextSibling 、previousSibling 来移动。
_READXML.NodeTree = function(xmlDoc,Nodename,index){
if(_READXML.NodeIsNull(xmlDoc,Nodename,index)){alert("error0008: 该节点不存在");return null;}
x=xmlDoc.getElementsByTagName(Nodename)[index].childNodes;
y=xmlDoc.getElementsByTagName(Nodename)[index].firstChild;
var arrTree=[];
for (i=0;i<x.length;i++)
{
arrTree.push(y.nodeName);
y=y.nextSibling;
}
return arrTree;
}
///获取通过节点的关系节点名称和值树中导航。
_READXML.NodeTreeValue = function(xmlDoc,Nodename,index){
if(_READXML.NodeIsNull(xmlDoc,Nodename,index)){alert("error0009: 该节点不存在");return null;}
x=xmlDoc.getElementsByTagName(Nodename)[index].childNodes;
y=xmlDoc.getElementsByTagName(Nodename)[index].firstChild;
var arrTreeValue=[];
for (i=0;i<x.length;i++)
{
if(_READXML.IsIE()){
arrTreeValue.push(y.nodeName+":"+y.text);
}else{
arrTreeValue.push(y.nodeName+":"+y.nodeValue);
}
y=y.nextSibling;
}
return arrTreeValue;
}
/某一节点长度
_READXML.GetNodeLength =function(xmlDoc,Nodename,index){
//if(_READXML.NodeIsNull(xmlDoc,Nodename,index)){alert("error0010:该节点不存在");return 0;}
if(_READXML.NodeIsNull(xmlDoc,Nodename,index)){return 0;}
var t=xmlDoc.getElementsByTagName(Nodename);
return t.length;
}
///判断某一节点是否为空节点
_READXML.IsHaschildNode =function(xmlDoc,Nodename,index){
if(_READXML.NodeIsNull(xmlDoc,Nodename,index)){alert("error0011:该节点不存在");return false;}
t=xmlDoc.getElementsByTagName(Nodename)[index];
return t.hasChildNodes()==true?true:false;
}
///添加节点
_READXML.AddNode = function(xmlDoc,xmlFile,FatherNodename,index,childNodename,childNodeText,id){
try{
if(_READXML.NodeIsNull(xmlDoc,FatherNodename,index)){alert("error0012:该节点不存在");return false;}
var newNode=xmlDoc.createElement(childNodename);//创建元素节点
if(childNodeText=="" ||childNodeText==null){;}else{
var newtext=xmlDoc.createTextNode(childNodeText);
newNode.appendChild(newtext);
}
if(id==""||id==null){;}else{newNode.setAttribute("id",id);}
fatherNode=xmlDoc.getElementsByTagName(FatherNodename);//找到父节点
fatherNode[index].appendChild(newNode);//把newNode作为子节点追加到父节点
//
return(_READXML.saveXML(xmlDoc,xmlFile,true,true));
}catch(Err){
alert("ERROR0004:"+Err.message);return false;
}
}
/删除节点
_READXML.removeChildNodes = function(xmlDoc,xmlFile,Nodename,NodeValue,index){
var delNodename=xmlDoc.getElementsByTagName(Nodename);
var obj =null;
var delNodeValue ="";
//if(NodeValue==""||index==""||NodeValue==null||index==null){return false;}
if(index!=""||index!=null){
//var index=index.replace(/\D/g, "");//字符串转数值
obj =xmlDoc.getElementsByTagName(Nodename)[index];
obj.parentNode.removeChild(obj);
return(_READXML.saveXML(xmlDoc,xmlFile,true,true));
}
for(var i=0;i<delNodename.length;i++)
obj =xmlDoc.getElementsByTagName(Nodename)[i];
if(obj!=null)
{
if(_READXML.IsIE()){
delNodeValue =obj.childNodes[0].text;
}else{
delNodeValue =obj.childNodes[0].nodeValue;
}
if(delNodeValue==NodeValue){
obj.parentNode.removeChild(obj);
return(_READXML.saveXML(xmlDoc,xmlFile,true,true));
}
}
return false;
}
///改变节点属性或创建新属性,IE9以下不支持此方法
_READXML.setAttribute =function(xmlDoc,Nodename,index,nodeAttribue,newAttribue){
if(_READXML.NodeIsNull(xmlDoc,Nodename,index)){alert("error0006:该节点不存在");return false;}
x=xmlDoc.getElementsByTagName(Nodename)[index];
x.setAttribute(nodeAttribue,newAttribue);
}
用FSO保保存文件,非IE不能用
_READXML.saveXML =function(xmlDoc,xmlFile,overwrite,unicode){
//overwrite :如果文件存在,提示是否覆盖,unicode文本是否存为unicode文本;缺省为false;
var contentText ="";
if(typeof xmlDoc.xml=='undefined'){alert("error0020:不是XML文件");return false}
try{
contentText =xmlDoc.xml;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file =fso.CreateTextFile(xmlFile,overwrite,unicode);
file.Write(contentText);
file.close();
return true;
}catch(Err){
alert("Error0005:"+Err.message);return false;
}
}
PS:出于安全考虑,主流浏览器不支持xmlDoc.save()方法,只好用FSO方法来保存XML文件,闲来无事写这个东西,也不知道这个类有啥用处。