Loading XML into a DOM Document

Before you can do anything with DOM, you will have to set a reference to the MSXML object library. To do so, open the Visual Basic Editor and select Tools O References. In the References dialog box, select the latest version of Microsoft XML, as demonstrated in Figure 12-9.

References - VBAProject m

Available References:

Microsoft XMLj v2.6 [a|

Microsoft XML, v3.0 Microsoft XMLjVt.O

Microsoft XML, version 2.0 Microsoft.Vsa.dll MimeDir 1.0 Type Library mmAEPIugln 1.0 Type Library MMC Internal Web Browser event sink 1,0 Type Libre MMFUtil 1.0 Type Library MMRadioEngine 1.0 Type Library MMS Application Library MOFL 2.0 Type Library MnhvfiRrnw^fir 1 .n Tvne I ihrsrv




Microsoft XML, v5.0 -

Location: C:\Program Files\Common Files\Microsoft Shared\OFFICEll\M Language: Standard

Figure 12-9

You will notice that there are four different versions of MSXML shown in Figure 12-9. A fifth version (Microsoft XML, v6.0) may be available to you if you have installed SQL Server 2005. You generally want to set your reference to the latest version available. However, if you are building XML-based solutions that will be distributed to other users, you will want to take into account the version of Internet Explorer your users have installed. If there is a good chance that some of your users are using Internet Explorer 5.0, you will want to set the reference to Microsoft XML, v3.0.

In any case, the differences between the various versions, for the most part, will not impact most of the tasks you as an Excel developer will need to accomplish. For instance, you can be confident that the rest of the procedures in this chapter would run fine with Microsoft XML, v3.0. If you have a burning need to find out what are the exact differences between the various versions of MSXML, you can find out here: http://windowssdk.msdn.microsoft.com/en-us/library/ms753751.aspx.

Once your reference is set, take a look at the code shown here. In this procedure, you are loading an XML file into a DOMDocument object. The DOMDocument object is the top-level container for the parsed XML file, serving as the parent for all nodes in the XML file's node structure. To programmatically explore and manipulate XML documents, they must first be exposed through the DOMDocument object.

Sub Load_ReadXMLDoc() Dim oMyDoc As DOMDocument

'Create an instance of the DOMDocument Set oMyDoc = New DOMDocument

'Disable asynchronous loading oMyDoc.async = False

'Load XML information from a file oMyDoc.Load (ThisWorkbook.Path & "\SalesByRegion.xml")

'Use the DOMDocument object's XML property to retrieve the raw data Debug.Print oMyDoc.XML


Set oMyDoc = Nothing

End Sub

First, instantiate a DOMDocument object, assigning it to the MyDoc variable. Next, disable asynchronous loading. Asynchronous loading is a process that MSXML uses by default to load documents in stages, allowing for cancellation and feedback during load. You generally will want to disable asynchronous loading to ensure that the document loads in its entirety before a result is returned.

Use the Load method to load the chosen XML document into the DOMDocument object. This essentially takes a snapshot of the XML document and loads an in-memory version of the XML document that you can explore and modify via VBA. In this example, you retrieve the raw XML data from the document and output it to the Immediate window. After you are done with any DOMDocument, you should always release it from memory.

Creating And Writing Your Blog

Creating And Writing Your Blog

The success of a blog lies in the content. It is the content and the key words that consistently attract potential customers. Great blog content literally means something different on every blog. Great blog content is researched and not impulsive. You cannot treat the blog as an experimental playground and expect it to work for you. It is designed to mean business and make you presence felt online and you have to ensure it performs the way you want it to.

Get My Free Ebook


  • martin
    How to add reference of a library reference to msxml v. 3 in vba?
    7 years ago
  • luisella endrizzi
    What reference do need for a domdocument?
    3 months ago

Post a comment