XML and javascriptXML in javascript

Source: Internet
Author: User

XML and javascriptXML in javascript

Create DOM in IE and load XML

Var xmldoc = new ActiveXObject ("Microsoft. XMLDOM "); xmldoc. load (url); // load the XML file xmldoc. loadXML ("<root> <son/> </root>"); // loads the xmldoc string xmldoc.doc umentElement // gets the root element, only supported by IE

Create DOM in non-IE environment and load XML

Var xmldoc = document. implementation. createDocument ("", "", null); xmldoc. async = true; // asynchronously load xmldoc. load (url );

The first parameter specifies the file namespace URL, the second parameter is used to specify the file element tag name, and the third label is used to specify the document type object, which is generally null

Example

var xmldoc =document.implementation.createDocument("http://www.x-do.org","root",null);

This line of code creates an xml dom that represents <rootxmlns = "http://www.x-do.org"/>

 

The W3C standard xml dom does not support the loadXML () method. To parse an XML string to DOM, you must use the DOMParser object and use its parseFromString method to pass in the XML string representation:

Var xmlParser = new DOMParser (); var xmlDom = xmlParser. parseFromString ("<root/>", "text/xml"); // This method returns an XMLDOM object // The second parameter text/xml can also be application/xml, both are used to parse XML // or application/xhtml + xml, which is used to parse XHTML. Only these three MIME types can be used.

Methods for obtaining XML strings that correspond to direct parsing of XML strings. xml dom objects in IE have read-only xml attributes, while W3C standards do not, however, an XMLSerializer object that can be used for the same purpose is provided:

 

Var serializer = new XMLSerializer (); var xmlStr = serializer. serializeToString (xmlDom, "text/xml"); // xmlDom is an xml dom Node object // and text/xml can also be application/xml
 

You can bind a function to the onload event (for W3C compatible browsers) or onreadystatechange event (for Windows Internet Explorer) of the XML document object ). Then, the bound function can be used to process the content of the XML document.

Compatible creation of xml dom objects

if (typeof document.implementation.createDocument != "undefined") {      xmldoc =document.implementation.createDocument("", "", null);      xmldoc.onload = displayData;} else if(window.ActiveXObject) {      xmldoc = new ActiveXObject("Microsoft.XMLDOM");      xmldoc.onreadystatechange = function () {             if (docObj.readyState == 4)             displayData();      };}xmldoc.load(url);function displayData(){   }
 

The load () method can only load files stored on the same server as pages containing JavaScript. That is to say, XML files cannot be loaded on other people's servers.

When a file is loaded in synchronous mode, the JavaScript code will wait until the file is fully loaded before executing the Code; while loading in asynchronous mode, it will not wait, you can use the event processing function to determine whether the file is fully loaded. By default, files are loaded in asynchronous mode. To perform synchronous loading, set async to false.


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.