验证XML文件的合法性只限IE

 

    var xt = "",
	h3OK = 1

	function checkErrorXML(x) {
		xt = ""
		h3OK = 1
		checkXML(x)
	}

	function checkXML(n) {
		var l, i, nam
			nam = n.nodeName
		if (nam == "h3") {
			if (h3OK == 0) {
				return;
			}
			h3OK = 0
		}
		if (nam == "#text") {
			xt = xt + n.nodeValue + "\n"
		}
		l = n.childNodes.length
		for (i = 0; i < l; i++) {
			checkXML(n.childNodes[i])
		}
	}

	function validateXML(txt) {
		// code for IE
		if (window.ActiveXObject) {
			var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = false;
			xmlDoc.loadXML(document.all(txt).value);

			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);
			} else {
				alert("没有找到错误!");
			}
		}
		// code for Mozilla, Firefox, Opera, etc.
		else if (document.implementation.createDocument) {
			var parser = new DOMParser();
			var text = document.getElementById(txt).value;
			var xmlDoc = parser.parseFromString(text, "text/xml");

			if (xmlDoc.getElementsByTagName("parsererror").length > 0) {
				checkErrorXML(xmlDoc.getElementsByTagName("parsererror")[0]);
				alert(xt)
			} else {
				alert("没有找到错误!");
			}
		} else {
			alert('您的浏览器不支持 XML 验证器');
		}
	}I

  

posted @ 2022-10-09 11:22  奥兰王子  阅读(57)  评论(0编辑  收藏  举报