What is XML DOM?
The Document Object Model (DOM) as implemented in MSXML provides a programmatic representation of XML documents, fragments, nodes, or node-sets. It also provides an application programming interface for working with XML data. As an XML representation, it conforms to the W3C DOM specification. As a set of API, XML DOM objects are COM objects that implement interfaces and can be used in XML applications written in programming languages such as C/C++, Visual Basic, VBScript, and JScript.
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");
- 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 = "<a><a1>1</a1><a2>2</a2></a>"; 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 empty
<a3>
element as a new child of the document element,<a>
: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"); 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.
See Also
Concepts | How Do I? | DOM Reference
Other Resources
W3C Document Object Model (DOM) Level 1 Specification (Second Edition)