jquery static method Parsexml use and source code analysis

Source: Internet
Author: User

Jquery.parsexml (data) accepts a well-formed XML string that returns the parsed XML document.

Method Jquery.parsexml () is implemented using a browser-native XML parsing function.

In IE + + and other browsers, Domparser object parsing is used, and in browsers under IE 9, the ActiveXObject object is used to resolve

SOURCE Analysis

//cross-browser XML parsingParsexml:function(data) {varXML, TMP; Try {            if(Window. Domparser) {// StandardTMP =NewDomparser (); XML= tmp.parsefromstring (data, "Text/xml") ); } Else{//IEXML =NewActiveXObject ("Microsoft.XMLDOM" ); Xml.async= "false";            Xml.loadxml (data); }        } Catch(e) {XML=undefined; }        if(!xml | |!xml.documentelement | | xml.getelementsbytagname ("parsererror"). Length) {Jquery.error ("Invalid XML:" +data); }        returnXML; },

First, for IE (9 or less) and the world standard to distinguish between the processing, for the Web browser to adopt the standard parsing object Domparser, the following for this object to expand

// The Domparser object parses the XML text and returns an XML Document object.  // to use Domparser, instantiate it with a constructor without arguments, and then call its parsefromstring () method:var doc = (new Domparser ()). parsefromstring (text)
Parsefromstring is defined as follows
// The text parameter is the XML tag to parse // ContentType is the content type of the text. May be one of "Text/xml", "Application/xml" or "Application/xhtml+xml". Note that the "text/html"parsefromstring (text, contentType) is not supported

IE uses the ActiveXObject object parsing XML, an IE-specific method that parses the specified XML text string, and then constructs a DOM node tree in the current document object, discarding any nodes that previously existed in the document.

This method does not exist on document objects that represent HTML documents. Before calling LoadXML (), a new, empty document object is typically created to hold the parsed content:

var New ActiveXObject ("MSXML2. DOMDocument ");d Oc.loadxml (markup);

IE9 the following browser resolves the Document object after parsing has failed this object is null

Parsing failure above IE9 error executing a catch statement to assign XML value to undefined

In browsers other than IE, if parsing fails, the method parsefromstring () does not throw any exceptions and only returns a Document object that contains the error message, as follows:

<xmlns= "http./www.mozilla.org/newlayout/xml/parsererror.xml"> (Error description) < SourceText > (a snippet of the source XML) </ SourceText > </ ParserError >

Based on the following, we use a few judgment conditions to verify the success of the analysis

if (!xml | |!xml.documentelement | | xml.getelementsbytagname ("parsererror"). Length) {            jquery.error ("Invalid xml: "+ data);}

1.xml conversion to false such as IE9 parsing failure is

2.xml.documentelement can be converted to false for IE low version

3.parsererror element exists for non ie

Satisfying any one will invoke the error method to tell the developer that XML is invalid, and then return the parsed XML document

jquery static method Parsexml use and source code analysis

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.