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 that support COM.


The following PowerBASIC code 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 PowerBASIC code fragment is an example.



pXMLDoc = NEWCOM "Msxml2.DOMDocument.6.0"


Next, you can load XML data from a file into the DOM object, as follows:


pXMLDoc.load "books.xml"


Alternatively, you can load data from an XML stream that originated from another application, or that was created dynamically:


bstrXML = UCODE$("<a><a1>1</a1><a2>2</a2></a>")

pXmlDoc.loadXml bstrXML


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:



pNode = pXmlDoc.selectSingleNode(UCODE$("//a2"))


To insert a new element into a DOM object, you set properties and call 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>:


DIM pRootElement AS IXMLDOMElement

DIM pNewElement AS IXMLDOMElement

pRootElement = pXmlDoc.documentElement

pNewElement = pXmlDoc.createElement(UCODE$("a3"))

pRootElement.appendChild pNewElement


To persist a DOM object, call the save method on the object:


pXmlDoc.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:


DIM pXslt AS IXMLDOMDocument

pXslt = NEWCOM "Msxml2.DOMDocument.6.0"

pXslt.load "transform.xsl"

bstrXML = pXmlDoc.transformNode(pXslt)


These are just a few simple examples to show you how DOM can be used to work with an XML document.


Valid XHTML 1.0 Transitional