February 8, 1999
Microsoft Tools Offering More Support For XMLComponent supports Visual Basic 6.0, makes data easier to use in applications
By Don Kiely
icrosoft has long been a proponent of the Extensible Markup Language, and has served on the standards committee for the technology. The company's support of this emerging standard, and its implementation of early versions in its many development products, will help accelerate industry acceptance of this data exchange format.Internet Explorer 4.0 was the first product to use Microsoft's XML dynamic link library and to make those features available to outside developers. Now, months after the World Wide Web Consortium has blessed the XML standard and on the eve of the release of Internet Explorer 5.0, Microsoft has introduced an updated MSXML.DLL. Besides closer adherence to the various approved and proposed standards (see code example at right), the new version of MSXML has a number of features that make it easier for developers to create applications that use XML.
The latest release includes searching capabilities to support complex queries of XML data. New schema features allow explicit use of data types, and allow aggregation of existing schemas into more complex schemas. They support XSL for formatting data, but also support a default style sheet to display data when no XSL file is specified.
The core enabling technology in MSXML is the XML parser, which Microsoft has released in beta form in C++ and Java. The parser is the means by which developers access XML's object model.
The XML parser reads the data in an XML document and makes its components available through the XML object model or through data-binding features. The parser can handle either well-formed or valid XML documents. The latter requires a document type definition file, which specifies the required structure of a document.
MSXML and IE 5.0 support data islands--XML data embedded within a regular HTML page. This is the feature that supports the tags in Visual Basic 6.0 WebClass HTML templates. Data islands allow text replacement using the ProcessTag event procedure. But IE can also display XML data, formatted by an XSL document if one is available, as one of its document types.
XML Object Model
The key to using Microsoft's implementation of XML is understanding the object model used to programmatically access and manipulate data in the document. Unlike some object models Microsoft has created, the XML model is a true hierarchy reflecting the specifications of the proposed XML Document Object Model standard. Since the XML Document Object Model standard has not been finalized by the W3C, there's a pretty good chance it will change and Microsoft will have to change its model accordingly.
At the top of the hierarchy is the XML Document object, corresponding to an entire document with data and XML tags. The root node amounts to the first opening XML tag in the document, consisting of the opening and closing tags that encompass the remaining contents of the document. Contained within the root node are any number of node objects, nested to any level.
| Confusing Object Names In MSXML | |
IXMLDOMNodeList IXMLDOMNode DOMDocument Other objects with DOM prefix: DOMDocument and DOMFreeThreadedDocument Other objects with IXML prefix |
NodeList Node XML Document Base interfaces that support the W3C Document Object Model COM interfaces exposed as ActiveX properties |
XML Meets Visual Basic
Using MSXML in a VB application is just like using any ActiveX component. It must be installed on the developer PC, and the only way to get it there is by installing IE 5.0. In a VB project, add a reference to "Microsoft XML, version 2.0" (if version 1.0 is listed, you have the old version that came with IE 4.0).
Once the reference is set, the developer can begin declaring object variables for the various XML objects. But here is where things get interesting. If the user opens VB's object browser and selects the MSXML library, a rather confusing set of objects appears. The chart "Confusing Object Names" (above) shows which objects correspond to the XML object model. In general, objects prefaced with DOM or IDOM expose the XML document as a tree structure consisting of nodes, letting an application traverse the tree and manipulate its data. Objects prefaced with XML or IXML support the W3C document object model, as well as extensions Microsoft has made to enhance the ability of applications to work with XML data.
But in MSXML's current form, the naming conventions are inconsistent and the XML Document object is labeled as obsolete. Hopefully, this is just a temporary by-product of the beta version and will be sorted out before final release.
Using MSXML with VBScript, which supports only Variant data types, requires the use of the CreateObject function and a generic object variable:
|
In VB applications, you can use typed object variables, so that you define the type of object that the object refers to before using it for the first time. Here are the variable declarations for the high-level objects that MSXML exposes to VB applications:
|
An application will typically make use of the DOMDocument object most often to load and parse an XML document and provide access to nodes containing data.
The Load method opens the document and prepares it for further manipulation:
|
The Load method takes a URL as a parameter, which can be a fully formed name of a local disk file. A related method, LoadXML, lets you pass an XML document directly as a string.
Digging Into XML Data
Once you have an XML document loaded, you can begin manipulating, querying, and updating its data. The documentElement property of the DOMDocument object provides access to the root node of the XML document. The childNodes collection under each node gives access to the next lower level of nodes. The code example on p. 86 is taken from a Microsoft sample application. It loops through an XML document, extracts a list of customer and company names, and adds them to a cmbCustomer combo box on a form.
An XML document gathers identification information about customers. The For statement in the VB code loops through each element in the second level of nodes, which contains the
tags. If the company name (item 0 within the customer data) is blank, it concatenates the customer's first and last names, adds the customer's ID contained within the ID attribute, and adds the string to the combo box. If the company name is not blank, it puts it at the beginning of the string.
The code is messy because each reference to data in the document drills down into lower and lower node levels, using the VB dot operator. In normal use, the code would be more efficient either by using With...End With blocks or by defining object variables that contained references to the appropriate collections.
All the benefits of XML documents are worthless if the data can't be searched and queried. The code example at right queries that data for the product name the user has selected. After defining the search string and identifying the child node desired, the code sets the qryResult string to the root node. Using that as the starting point, it executes the query using the selectSingleNode method, passing the sQuery string as a parameter. If the item is located, qryResult contains a reference to the Node object, which can be read or updated as needed. To update the data, set qryResult.Text to the new value.
If any changes are made to the document, it can be saved to disk using VB's regular low-level file functions. The Document objects XML property returns a string with the full contents of the document.
Committed To XML
Microsoft seems to be aggressively expanding its use of XML throughout its developer tools. ActiveX Data Objects 2.0 supports XML, and MSXML will be distributed with Windows 2000. This will allow Web pages viewed in IE 5.0 and other applications to bind data to controls on a form as a native means to bind an application to XML data. It also means that installing this latest version of Microsoft's browser will no longer be a requirement of using the MSXML component. Microsoft is pursuing an XML Schema that will allow data to be typed in an XML document, presumably allowing nonstring data.
MSXML is still a work in progress, with conflicting and confusing object names, some of which are prefaced as though they were interfaces. In addition, the documentation seems to have been written by Java developers but aimed at VB programmers. But the XML component is shaping up to be fully compliant with the approved W3C standards, while adding extensions that make XML data easier to use in applications.
Microsoft's support for XML, including its inclusion with the forthcoming Windows 2000, is only going to speed the acceptance of the language as a data-exchange format that transcends platforms and applications.
Read sidebar story, "Pros and Cons Of Microsoft's XML Object Model."
This Week's Issue
Free Print Subscription
SubscribeSupplemental Issue
Related Whitepapers
- Forrester Research study How Blade Servers Impact Datacenter Management and Agility
- ComputerWorld Tech Dossier: HP ProLiant DL360p & DL380p Gen8 Severs: Power, Flexibility & Serviceability
- ComputerWorld Tech Dossier HP ProLiant Gen8 Servers: Intelligent Mgmt and Greater Efficiency Throughout the LifeCycle
- Virtualizing Tier 1 Applications: A Critical Step on the Journey Toward the Private Cloud
- Meeting the Challenges of Endpoint Security
Related Reports
Related Webcasts
- Cloud or Premise Based Contact Center – Which is Right [for YOU]?
- Techniques for Next-Gen Data Protection using Next-Gen Computing
- Enhance Business Performance with Process Oriented Data Stewardship
- Future Proofing your Video Communication Strategy
- The view is better up here: breaking through barriers to Cloud











