日期:2014-05-16  浏览次数:20380 次

JavaScript验证XML

/**
 * 支持chrome, firefox, ie的xml验证
 */
function validateXML(txt){
	// code for IE
	if (window.ActiveXObject){
	  var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	  xmlDoc.async="false";
	  xmlDoc.loadXML(txt);
	
	  if(xmlDoc.parseError.errorCode!=0){
		//txt="Error Code: " + xmlDoc.parseError.errorCode + "\n";
		//txt=txt+"Error Reason: " + xmlDoc.parseError.reason;
		//txt=txt+"Error Line: " + xmlDoc.parseError.line;
		//alert(txt);
		//Ext.Msg.alert("警告","XML验证错误!");
		return false;
	  }else{
		//alert("XML验证通过!");
		return true;
	  }
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation.createDocument){
		var parser=new DOMParser();
		var xmlDoc=parser.parseFromString(txt,"text/xml");
	
		if (xmlDoc.documentElement.nodeName=="parsererror" || xmlDoc.documentElement.firstChild.nodeName=="parsererror"){
			//Ext.Msg.alert("警告","XML验证错误!");
			return false;
		}else{
			//alert("XML验证通过!");
			return true;
		}
	}
}