The XML in JavaScript

Source: Internet
Author: User
Tags xml attribute

Create DOM and load XML under IE

var xmldoc = new ActiveXObject ("Microsoft.XMLDOM"); xmldoc.load (URL);  Load XML file Xmldoc.loadxml ("<root><son/></root>");//load XML string xmldoc.documentelement//Get root element, ie support only

Create DOM and load XML in non-IE

var xmldoc =document.implementation.createdocument ("", "", null); Xmldoc.async =true;    Asynchronously loading the xmldoc.load (URL);

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

Cases

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

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

The Loadxml () method is not supported by the standard XML DOM. To parse an XML string into the DOM, you must use the Domparser object, using its Parsefromstring method, to pass in the XML string representation:

      var xmlparser = new Domparser ();      var xmlDom =xmlparser.parsefromstring ("<root/>", "text/xml");  The method returns a Xmldom object      //second parameter text/xml can also be application/xml, both of which are used to parse the XML      //And can also be application/xhtml+xml, used to parse XHTML, Only use these three kinds of mime

The XML DOM object in IE has a read-only XML attribute that corresponds to the direct parsing of the XML string, and the XmlSerializer object has no corresponding attributes, but it provides the same objects that can be used for the same purpose:

    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 of an XML Document object (for a web-compatible browser) or onReadyStateChange event (for Windowsinternet Explorer). The bound function can then be used to manipulate the contents of the XML document.

Creating XML DOM objects in a compatible manner

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 that are stored on the same server as pages that contain JavaScript, that is, you cannot load an XML file from another person's server

When you load a file in synchronous mode, the JavaScript code waits for the file to fully load before continuing to execute the code, while loading in asynchronous mode does not wait, you can use the event handler to determine whether the file is fully loaded. By default, files are loaded in asynchronous mode. To perform a synchronous load, simply set the async attribute to False


The XML in JavaScript

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.