Method for verifying the correctness of the Javascript front-end XML format

Source: Internet
Author: User
I. Principle:

In view of the fact that we have recently completed a front-end XML data verification, found some information, and made some summary, and wrote a simple XML string verification function, the implementation process is mainly to parse the XML string by using the XML parser. If the parsing error occurs, the corresponding parsing error will be reported. This idea is used to verify the correctness of the XML format.

XML parsing is different in IE and non-ie browsers. Therefore, two methods are required for parsing. in IE, activexobject ("Microsoft. xmldom ") objects can be used for parsing. In non-ie, domparser objects can be used for parsing.
1.1 in IE

Xmldoc = new activexobject ("Microsoft. xmldom"); xmldoc. async = "false"; xmldoc. loadxml (xmlcontent); If (xmldoc. parseerror. errorcode! = 0) {errormessage = "error code:" + xmldoc. parseerror. errorcode + "\ n"; errormessage = errormessage + "error cause:" + xmldoc. parseerror. reason; errormessage = errormessage + "error location:" + xmldoc. parseerror. line; errorcode = 1;} else {errormessage = "The format is correct ";}

I believe this code is not hard to understand. Most of it is the attributes of XML objects and error information is obtained. 1.2. Non-ie (Mozilla, Firefox, opera, chrome, Safari) is divided into two parts, the main reason is that Mozilla, Firefox, and opera are incorrectly parsed, parsefromstring returns a document object, but the document element of this object is <parsererror>
For example:

:<parsererror><span id="xml_error" style="display: block; ">

Such a string of error messages.
If the document returned by Safari and chrome contains the <parsererror> label, but the element only shows the current error, the xmldoc.doc umentelement. nodename method cannot be used to obtain the parsing error,
A more common method is required, that is, xmldoc. getelementsbytagname ("parsererror") to check whether this tag exists. The method for obtaining an error is different.

 if(xmlDoc.documentElement.nodeName=="parsererror"){                errorCode = 1;                errorMessage = xmlDoc.documentElement.childNodes[0].nodeValue;            } else {                errorCode = 1;                errorMessage = xmlDoc.getElementsByTagName("parsererror")[0].innerHTML;            }

If statement to determine whether it is Mozilla, Firefox, operabrowser, if yes, you can directly get the error information, while safari and Chrome browser need to get the innerhtml value of the tag
2. The code is king. The following is the main code implementation.

/** Verify the correctness of XML format */function validatexml (xmlcontent) {// errorcode 0 indicates that XML is correct, 1 indicates XML error, 2 indicates that VaR xmldoc, errormessage cannot be verified, errorcode = 0; // code for IE if (window. activexobject) {xmldoc = new activexobject ("Microsoft. xmldom "); xmldoc. async = "false"; xmldoc. loadxml (xmlcontent); If (xmldoc. parseerror. errorcode! = 0) {errormessage = "error code:" + xmldoc. parseerror. errorcode + "\ n"; errormessage = errormessage + "error cause:" + xmldoc. parseerror. reason; errormessage = errormessage + "error location:" + xmldoc. parseerror. line; errorcode = 1;} else {errormessage = "The format is correct"; }}// code for Mozilla, Firefox, opera, chrome, Safari, etc. else if (document. implementation. createdocument) {var parser = new domparser (); xmldoc = parser. parsefromstring (xmlcontent, "text/XML"); var error = xmldoc. getelementsbytagname ("parsererror"); If (error. length> 0) {if(xmldoc.doc umentelement. nodename = "parsererror") {errorcode = 1; errormessage = xmldoc.doc umentelement. childnodes [0]. nodevalue;} else {errorcode = 1; errormessage = xmldoc. getelementsbytagname ("parsererror") [0]. innerhtml ;}} else {errormessage = "correct format" ;}} else {errorcode = 2; errormessage = "the browser does not support verification and cannot verify XML correctness ";} return {"MSG": errormessage, "error_code": errorcode };}// the code can be found in others' XML parsing documents.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.