HandsOn Loading and Retrieving the Contents of an XML File

1. In the Visual Basic Editor screen, choose Insert | Module to add a new standard module to the current VBA project.

2. In the module's Code window, enter the ReadXMLDoc procedure shown below.

Sub ReadXMLDoc()

Dim xmldoc As MSXML2.D0MDocument50 Set xmldoc = New MSXML2.D0MDocument50

xmldoc.async = False

If xmldoc.Load("C:\Learn_XML\Shippers.xml") Then Debug.Print xmldoc.XML ' Debug.Print xmldoc.Text End If End Sub

To work with an XML document, we begin by creating an instance of the DOMDocument object as follows:

Dim xmldoc As MSXML2.D0MDocument50 Set xmldoc = New MSXML2.D0MDocument50

The MSXML uses an asynchronous loading mechanism by default for working with documents. Asynchronous loading allows you to perform other tasks during long database operations, such as providing feedback to the user as MSXML parses the XML file or giving the user the chance to cancel the operation. Before calling the Load method, however, it's a good idea to set the Asynch property of the DOMDocument object to False to ensure that the XML file is fully loaded before other statements are executed. The Load method returns True if it successfully loaded the data and False otherwise. Having loaded the XML data into a DOMDocument object, you can use the XML property to retrieve the raw data or use the Text property to obtain the text stored in document nodes.

3. Position the insertion point anywhere within the code of the ReadXMLDoc procedure and choose Run | Run Sub/UserForm. The procedure executes and writes the contents of the XML file into the Immediate window as shown in Figure 29-22.

Note: For this procedure to work correctly, you must set up the reference to the Microsoft XML object type library as instructed at the beginning of this section.

Taking Your VBA Programming Skills to the Web

Taking Your VBA Programming Skills to the Web

■idataiooi >:iilM5 u : 3ilLct&5-silctoscvc c on: dCClceiiata" >:iilrj3:x31="hccii: //ww.va.oLir/'ZQOlAn] iShipperax iShipp e r Ii»l</Shipp e r II» <coiili:ajiyjlaije>.jBeeily E k p r e s s< / C oai aEytfaiie > <Ph(me>(£03) 555-9a 3li/Phonex i/Shipper X: XX'.XX

XShipp e r njxxx/xbipp 11ILX iCtmpQiiyHoiLexUnited Packagei/CanpculyHoklO ■XXi ; r. ' : 55 XX:* .X ¡.; ; ■ i/Shippersx ■ Shij p e ] ' ■

' <:r.:'." r ' *x:i:x: Xix xCcitp enyHeiiexFede lai Shippingi/C onp anyHamex XFlmiicX <5Ci3) 555-9 9 3 K/Phonex ' * 'x : xx " v ,;

X/datatootx

Figure 29-22: By using the XML property of the DOMDocument object you can retrieve the raw data from an XML file.

4. In the code of the ReadXMLDoc procedure, comment the first Debug.Print statement and uncomment the second statement that reads Debug.Print xmldoc.Text.

5. Run the ReadXMLDoc procedure again. This time the Immediate window shows the entry as one long line of text:

1 Speedy Express (503) 555-9831 2 United Package (503) 555-3199 3 Federal Shipping (503) 555-9931

As you already know, the XML DOM represents a tree-based hierarchy of nodes. An XML document can contain nodes of different types. For example, an XML document can include a document node that provides access to the entire XML document or one or more element nodes representing individual elements. Some nodes represent comments and processing instructions in the XML document, and others hold the text content of a tag. To determine the type of node, use the NodeType property of the IXMLDOMNode object. Node types are identified either by a text string or a constant.

For example, the node representing an element can be referred to as NODE_ELEMENT or 1, while the node representing the comment is named NODE_COMMENT or 8. See the MSXML2 Library in the Object Browser for the names of other node types.

In addition to node types, nodes can have parent, child, and sibling nodes. The hasChildNodes method lets you determine if a DOMDocument object has child nodes. There's also a childNodes property, which simplifies retrieving a collection of child nodes. Before you start looping through the collection of child nodes, it's a good idea to use the length property of the IXMLDOMNode object to determine how many elements the collection contains.

The following hands-on exercise uses the Shippers.xml file to demonstrate how to work with XML document nodes.

0 0

Post a comment