1<script>2 functionvalidatexml (xmlcontent)3 { 4 //errorCode 0 is xml correct, 1 is an XML error, 2 is not verifiable5 varXmldoc,errormessage,errorcode = 0; 6 //code for IE7 if(window. ActiveXObject)8 { 9xmldoc =NewActiveXObject ("Microsoft.XMLDOM"); TenXmldoc.async= "false"; One Xmldoc.loadxml (xmlcontent); A - if(xmldoc.parseerror.errorcode!=0) - { theErrormessage= "Error code:" + XmlDoc.parseError.errorCode + "\ n"; -Errormessage=errormessage+ "Error Reason:" +XmlDoc.parseError.reason; -Errormessage=errormessage+ "Error Location:" +XmlDoc.parseError.line; -ErrorCode = 1; + } - Else + { AErrorMessage = "properly formatted"; at } - } - //code for Mozilla, Firefox, Opera, Chrome, safari,etc. - Else if(document.implementation.createDocument) - { - varParser=NewDomparser (); inxmldoc = parser.parsefromstring (xmlcontent, "Text/xml"); - varError = Xmldoc.getelementsbytagname ("ParserError"); to if(Error.length > 0) + { - if(xmldoc.documentelement.nodename== "ParserError"){ theErrorCode = 1; *ErrorMessage = Xmldoc.documentelement.childnodes[0].nodevalue; $}Else { Panax NotoginsengErrorCode = 1; -ErrorMessage = Xmldoc.getelementsbytagname ("ParserError") [0].innerhtml; the } + } A Else the { +ErrorMessage = "properly formatted"; - } $ } $ Else - { -ErrorCode = 2; theErrorMessage = "Browser does not support validation and cannot validate XML correctness"; - } Wuyi return { the"MSG": errormessage, -"Error_code": ErrorCode Wu }; -}
Method invocation:
1 $ (document). Ready (function() {2 function() {3 $ (this). val () = = "" | |! Validatexml ($ (this). Val ()). Error_code? $ (this). Removeclass ("Bolder"): $ (this). AddClass ("bolder"); 4 }); 5 });
JavaScript validation XML format