The following JScript fragments outline the basic process of programming with XML DOM.
• To work with XML data programmatically, you first create an XML DOM object. The following JScript code fragment is an example
var xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
• Next, you can load XML data from a file into the DOM object, as follows:
xmldoc.load("file.xml");
• Alternatively, you can load data from an XML stream that originated from another application, or that was created dynamically:
strXML = "1 2 ";
xmldoc.loadXML(strXML);
• To navigate to a node in the XML document, you can specify an XPath expression in the call to one of several methods on the DOM instance, for example:
var node = xmldoc.selectSingleNode("//a2");
To insert a new element into a DOM object, you set properties and cal methods on the object, and possibly on its child objects. For example, the following code fragment appends an emptyelement as a new child of the document element, :
xmldoc.documentElement.appendChild( xmldoc.createElement("a3") );
• To persist a DOM object, call the save method on the object:
xmldoc.save( "new.xml" );
• To perform XSL Transformations (XSLT) on an XML document, you can create another DOM object to hold the XSLT style sheet, and then call the transformNode method on the DOM object for the XML document:
var xslt = new ActiveXObject("msxml2.DOMDocument.3.0");
xslt.load("transform.xsl");
strXML = xmldoc.transformNode(xslt);
These are just a few simple examples to show you how DOM can be used to work with an XML document. More detailed discussions and demonstrations are provided in the later sections of this guide.