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