Javascript验证xml格式
1 <script>
2function validateXML(xmlContent)
3 {
4//errorCode 0是xml正确,1是xml错误,2是⽆法验证
5var xmlDoc,errorMessage,errorCode = 0;
6// code for IE
7if (window.ActiveXObject)
8 {
9 xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
10 xmlDoc.async="false";
11 xmlDoc.loadXML(xmlContent);
12
13if(Code!=0)
14 {
15 errorMessage="错误code: " + Code + "\n";
16 errorMessage=errorMessage+"错误原因: " + ason;
17 errorMessage=errorMessage+"错误位置: " + xmlDoc.parseError.line;
18 errorCode = 1;
19 }
20else
21 {
22 errorMessage = "格式正确";
23 }
24 }
25// code for Mozilla, Firefox, Opera, chrome, safari,etc.
26else if (ateDocument)
27 {
28var parser=new DOMParser();
29 xmlDoc = parser.parseFromString(xmlContent,"text/xml");
30var error = ElementsByTagName("parsererror");
31if (error.length > 0)
32 {
33if(deName=="parsererror"){
34 errorCode = 1;
35 errorMessage = xmlDoc.documentElement.childNodes[0].nodeValue;
36 } else {
37 errorCode = 1;
38 errorMessage = ElementsByTagName("parsererror")[0].innerHTML;
39 }
40 }
41else
42 {
43 errorMessage = "格式正确";
error parse new44 }
45 }
46else
47 {
48 errorCode = 2;
49 errorMessage = "浏览器不⽀持验证,⽆法验证xml正确性";
50 }
51return {
52 "msg":errorMessage,
53 "error_code":errorCode
54 };
55 }
⽅法调⽤:
1 $(document).ready(function(){
2 $('#xmlContent').bind('input propertychange', function() {
3 $(this).val() == "" || !validateXML($(this).val()).error_code ? $(this).removeClass("bolder") : $(this).addClass("bolder");
4 });
5 });
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论