现在的位置: 首页 > 综合 > 正文

js解析xml封装类

2013年12月05日 ⁄ 综合 ⁄ 共 2436字 ⁄ 字号 评论关闭
<script type="text/javascript">
	function XmlDom(){
		if(document.implementation&&document.implementation.createDocument){//w3c
			//第一个参数命名空间;第二个参数根节点
			this.xmlDom = document.implementation.createDocument("","",null);
			this.type = 'w3c';
		}else{//IE
			this.xmlDom = new ActiveXObject('Microsoft.XmlDom');
			
			this.type = 'IE';
		}
		
	}
	XmlDom.prototype = {
		loadFile:function(arg){//从本地或者远程载入xml文件
			/*
				arg:{
					url:加载路径,必选参数
					
					callback:加载完成后的回调函数,必选参数 
								function(xmlDom,XmlDom){}//xmlDom为得到的对象,使用w3c标准方法操作xml
														 //XmlDom 本身,可以使用扩展的getXml方法
								
					async:true为异步加载,false为同步加载,可选参数
				}
			*/
			arg.async = !!arg.async || true;

			if(this.type=='w3c'){//w3c
				if(arg.async){//异步加载
					var _this = this;
					this.xmlDom.onload = function(){
						if(this.documentElement.nodeName=='parsererror'){
							//w3c解析错误会用一个错误xml替换原始的xml
							throw new Error("XML Parse Error");
						}else{
							arg.callback(this,_this);
						}
					}
				}
				this.xmlDom.load(arg.url);//载入xml文件
				if(!arg.async){//同步加载
					arg.callback(this.xmlDom,this);
				}	
			}else if(this.type=='IE'){//IE
				
				var _this = this;
				if(arg.async){//异步加载
				
					this.xmlDom.onreadystatechange = function(){
						//这里面的this都指向window
						if(_this.xmlDom.readyState==4){
							if(_this.xmlDom.parseError.errorCode!==0){//解析有错
								throw new Error("XML Parse Error:"+_this.xmlDom.parseError.reason);
							}else{
								arg.callback(_this.xmlDom,_this);
							}
							
						}
					}
				}
				this.xmlDom.load(arg.url);
				if(!arg.async){
					arg.callback(this.xmlDom,this);
				}
			}
			
		},
		loadString:function(arg){
			/*
				arg:{
						s:xml字符串,必选参数
						callback:回调函数,必选参数
								 function(xmlDom,XmlDom){}
								 //xmlDom为得到的对象,使用w3c标准方法操作xml
								 //XmlDom 本身,可以使用扩展的getXml方法
				}
				
			*/
			
			
			if(this.type=='w3c'){
				var p = new DOMParser();
				var newDom=p.parseFromString(arg.s,"text/xml");
				if (newDom.documentElement.nodeName=="parsererror") {//有错
					throw new Error("XML Parse Error");
				}
				for (var i=0,n;i<newDom.childNodes.length;i++) {
					n=this.xmlDom.importNode(newDom.childNodes[i],true);
					//importNode用于把其它文档中的节点导入到当前文档中
					//true参数同时导入子节点
					this.xmlDom.appendChild(n);
				}
				arg.callback(this.xmlDom,this);
			}else if(this.type=='IE'){
				this.xmlDom.loadXML(arg.s);
				if (this.xmlDom.parseError.errorCode!==0) {
					throw new Error("XML Parse Error:"+this.xmlDom.parseError.reason);
				}
				arg.callback(this.xmlDom,this);
			}
		},
		getXml:function(xmlNode){//得到某个节点下面的xml文档
			if(this.type=='w3c'){
				var s= new XMLSerializer();
				return s.serializeToString(xmlNode,"text/xml");
			}else if(this.type=='IE'){
				
				return xmlNode.xml;
			}
		}
	};
</script>

<script type="text/javascript">
//示例
	var x = new XmlDom();
	var c = function(xmlDom,x){
				alert(xmlDom);
				//alert(x.getXml(xmlDom));
			};
	//x.loadFile({url:'test.xml',callback:c});
				
	var xmlStr="<root><book>知识分子的背叛</book><book>小逻辑"+
						"</book><book>不确定的科学和不确定的世界</book></root>";
	x.loadString({s:xmlStr,callback:c});
</script>

抱歉!评论已关闭.