Exercise: Using the DOM in Visual Basic

[Previous] [Next]

The DOM provides an interface into an XML document, allowing you to access the document's contents. Because the Microsoft XML processor is a COM object and contains support for the W3C DOM, you can instantiate it in any tool that can use COM objects. Thus you can use the XML processor in Microsoft Visual Basic, as in the this exercise, and you can use it in a Web browser function written in JavaScript, in a C++ program, or even in a Java application.

In this section, you will build a Visual Basic program that uses the DOM to access the contents of an XML document. The program instantiates a DOM object, loads an XML document into the object, creates a collection of elements, and iterates through the collection. You'll find all files—including the Architag XRay XML Editor—on the companion CD. Before you begin this exercise, install the editor from the XRaySetup.exe file.

The Microsoft implementation of the W3C DOM is contained in a DLL named msxml2.dll. This object is registered as a COM object as MSXML2.DOMDocument. You can use it wherever you can instantiate a COM object.

  1. Load mvp.xml document into the XRay XML Editor. You'll find this file in the \Samples\Ch05\ directory on the companion CD. You'll see the screen shown in Figure 5-2.
  2. Notice that Figure 5-2 is a well-formed XML document containing more than 150 entries. There is one entry for each baseball player who has won baseball's Most Valuable Player award. (For those of you who are counting, there have been three such awards: the Chalmers Award, given from 1911-1914; the League Award, given from 1922-1929; and finally the Baseball Writer's Award, also known as the MVP Award, first given in 1931.) Each entry in this example has a unique identifier on the entry element and seven elements describing the player. Our task is to calculate the batting average of each player that year and display the best and worst averages in a window.

    click to view at full size.

    Figure 5-2. The file mvp.xml in the Architag XRay XML Editor.

  3. Start Visual Basic as shown in Figure 5-3, click on Standard EXE, and then click Open.
  4. click to view at full size.

    Figure 5-3. Microsoft Visual Basic 6.0.

  5. Select Form1 from the Project Explorer window as shown in Figure 5-4. From the Project menu, choose Remove Form1.
  6. Figure 5-4. The Project Explorer window.

  7. Add the DOM form to the project by selecting Project, choosing Add File, and then choosing \Samples\Ch05\DOM.frm.
  8. Open the form by clicking the plus sign in the Project window. Double-click on the BaseballStats form, as shown in Figure 5-5.
  9. Figure 5-5. Getting to the BaseballStats form.

  10. Make this form the startup form by selecting Project1 Properties from the Project menu. Choose BaseballStats from the Startup Object pull-down menu, as shown in Figure 5-6. Click OK.
  11. click to view at full size.

    Figure 5-6. Making the BaseballStats form your startup form.

  12. From the Project menu, choose References. Add the MSXML parser to the project by choosing Microsoft XML, v3.0 from the Available References list, as shown in Figure 5-7. Click OK.
  13. click to view at full size.

    Figure 5-7. Adding the MSXML parser to your project.

    You should see an environment that looks like Figure 5-8.

    click to view at full size.

    Figure 5-8. The BaseballStats form.

  14. Double-click the form just to the right of the Compute button. This will bring up the Form Load subroutine. There are two lines missing. Enter them as follows:
  15.  Set oXMLDoc = CreateObject("MSXML2.DOMDocument") oXMLDoc.Load ("c:\mvp.xml") 

    Replace the path c:\ with the path to the mvp.xml file on your machine.

    Notice that the IntelliSense processor pops up all the available properties and methods when you key in the second line, as shown in Figure 5-9.

    click to view at full size.

    Figure 5-9. The IntelliSense processor shows all available properties and methods in a pop-up menu.

    Your subroutine should look like Figure 5-10 when you are finished.

    click to view at full size.

    Figure 5-10. The Form_Load subroutine.

    The first line creates an instance of the Microsoft XML parser, "Microsoft.XMLDOM". This parser is a COM object that you can use in any program. Think of XML's processing capabilities as a part of the operating system.

    The second line loads the MVP document into the object to make it available for scripting.

  16. Notice that at the top of the BaseballStats(Code) window there is a drop-down list box that displays Form. Click this, and select Compute. Doing so reveals the subroutine shown in Figure 5-11. This is the subroutine that will execute when the user clicks the Compute button.
  17. click to view at full size.

    Figure 5-11. The Compute_Click subroutine.

  18. Notice the blank line as the first line in the subroutine. Enter the following code:
  19.  Set Entries = oXMLDoc.selectNodes("//Entry") 

    As you enter code, you will see that the IntelliSense processor is helping you, as shown in Figure 5-12.

    click to view at full size.

    Figure 5-12. The IntelliSense processor helps determine the parameters of the selectNode method.

    The Compute_Click subroutine creates a collection of element nodes containing all 154 baseball player entries. Now we can access this collection one player at a time to find which players have the highest and lowest averages.

    Once we find the highest and lowest averages, the HighPlayer and LowPlayer objects contain the entire entries for the appropriate players. These objects are interrogated at the end of the subroutine and presented in the text box of the application.

  20. Start the program by pressing F5. You should see the window shown in Figure 5-13.
  21. click to view at full size.

    Figure 5-13. The main window of the MVP program.

  22. Click the Compute button, and see your beautiful results, shown in Figure 5-14.
  23. click to view at full size.

    Figure 5-14. The results of the MVP program.

  24. Close this window, and save the project as DOM.vbp.


XML and SOAP Programming for BizTalk Servers
XML and SOAP Programming for BizTalk(TM) Servers (DV-MPS Programming)
ISBN: 0735611262
EAN: 2147483647
Year: 2000
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net