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.
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.
Figure 5-2. The file mvp.xml in the Architag XRay XML Editor.
Figure 5-3. Microsoft Visual Basic 6.0.
Figure 5-4. The Project Explorer window.
Figure 5-5. Getting to the BaseballStats form.
Figure 5-6. Making the BaseballStats form your startup form.
Figure 5-7. Adding the MSXML parser to your project.
You should see an environment that looks like Figure 5-8.
Figure 5-8. The BaseballStats form.
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.
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.
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.
Figure 5-11. The Compute_Click subroutine.
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.
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.
Figure 5-13. The main window of the MVP program.
Figure 5-14. The results of the MVP program.