XML 10INETA
Home Up XML 10 MS XML DOM XML Attributes XML Links

 

 

XML 10-SECOND TUTORIAL: Objects and Interfaces


There are a number of different objects and interfaces 
for XML---MSXML.DOMDocument, IDOMDocument, XMLDocument, 
Microsoft.XMLDOM---but which object should you use with 
Visual Basic and Internet Explorer? 

The object library for XML can get a little confusing. 
It's worth understanding the nomenclature to make 
programming against the DOM easier. (DOM stands for 
Document Object Model. Check out 
http://www.xml.com/pub/r/Document_Object_Model_(DOM)_Requirements  
for more info on the DOM.) 

The principal progIDs (the classnames used in 
CreateObject() calls) all start with Microsoft:

Microsoft.XMLDOM creates an XML document.
Microsoft.XMLHTTP creates an XML HTTP Request object.
MSXML.DOMDocument is the older form of Microsoft.XMLDOM 
and shouldn't be used.

All of the derived interfaces, or what VB programmers 
would call a PublicNotCreateable object, start with the 
letter "I" for interface:

IXMLDOMElement is an element interface.
 IXMLDOMDocument is a document interface.
 IXMLDOMNodeList is a node-list interface. 

There's also a DOMDocument class, which is multi-use. It 
implements the same interface as IDOMDocument, but 
DOMDocument is a creatable class while IDOMDocument is a 
derived class. Thus, this VB code creates a new document 
and assigns an element to it:


Dim xmlDoc As New DOMDocument
Dim xmlNode As IXMLDOMElement

' Create Node Object
Set xmlNode=xmlDoc.CreateElement("document")

' Append Node Object
Set xmlDoc.AppendChild xmlNode

You should note that the IXMLDOMElement inherits from the 
IXMLDOMNode interface. This is important when dealing 
with Visual Basic because the IXMLDOMNode interface 
doesn't support the .selectNodes or .selectSingleNode 
methods. 

The odd part is that selectSingleNode itself returns an 
IXMLDOMNode object, but you can implicitly coerce it into 
an IXMLDOMElement by simple assignment:

Dim newNode As IXMLDOMElement
Set newNode = xmlNode.selectSingleNode("//myNode")

The newNode here is an IXMLDOMElement node, while the 
.selectSingleNode() function produces an IXMLDOMNode 
object.

Send mail to vblg@xocomp.net with questions or comments about this web site.
Copyright © 1998-2003 XOCOMP, llc
Last modified: 04/05/2002