Manipulating XML Documents Programmatically

You can create, access, and manipulate XML documents programmatically using the XML Document Object Model (DOM). The DOM has objects, properties, and methods for interacting with XML documents.

To use the XML DOM from your VBA procedures, take a few minutes now to set up a reference to the MSXML Object Library using the following steps:

1. Switch to the Visual Basic Editor window and choose Tools | References.

2. In the References window, select Microsoft XML, v5.0 (see Figure 29-20) and click OK.

Taking Your VBA Programming Skills to the Web

If you don't have version 5.0 installed, select the lower version of this object type library or upgrade your browser to the higher version so that the most recent library is available.

Figure 29-20: To work with XML documents pro-grammatically, you need to establish a reference to the Microsoft XML object type library.

Figure 29-20: To work with XML documents pro-grammatically, you need to establish a reference to the Microsoft XML object type library.

Now that you have the reference set, open the Object Browser and examine XML DOM's objects, methods, and properties (see Figure 29-21).

Figure 29-21: To view objects, properties, and methods exposed by the XML DOM (Document Object Model), open the Object Browser after setting up a reference to the Microsoft XML object type library (see Figure 29-20).

ta' Object Browser

mmm

|msxmi_2

3 .

<l I N i »I

1_

3 HI «I

Classes

Members of'DOMDocumenfôÛ'

iS DOMDocument

9

^ abort

a

0 DOMDocument26

apperidChild

bS DOMDocument30

eSP a sync

£0 DOMDocument4Q

& attributes

|y] DOMDotument50

M

of* baseName

■¿P DOMNodeTvpe

là1 child Nodes

IjSl DSOConlrol

^ cloneNode

IS DSOCon1ro126

create Attribute

Jfil DSOCon1ro130

createCDATASsclion

& DSOConlroMO

createComment

& DSOControlSO

ä createDocumenlFragment

SS FreeThreadedDOMDotur

createElement

FreeThreadedDOMDocur

a reateEntityR efere nee

J& FreeThreadedDOMDocur

ereateNode

V

I Class DOMDocumentso

a

1 Member 01 m5xml ?

I W3C-D0M XML Document (Apartment)

V

Figure 29-21: To view objects, properties, and methods exposed by the XML DOM (Document Object Model), open the Object Browser after setting up a reference to the Microsoft XML object type library (see Figure 29-20).

As mentioned earlier in this chapter (see Part 4 of Custom Project 29-2), the DOMDocument object is the top level of the XML DOM object hierarchy. This object represents a tree structure composed of nodes. You can navigate through this tree structure and manipulate the data contained in the nodes by using various methods and properties. The hands-on exercises in the following sections demonstrate how to read and manipulate XML documents by using VBA procedures.

Part V

0 0

Post a comment