Home > Articles > Web Services > XML

  • Print
  • + Share This

Parsing XML

The NT service application must parse the XML documents and extract their data in order to save it in a SQL database. Fortunately, Microsoft released an XML parser with IE 5: called MSXML 2.0. This DLL provides a COM object model as well as parsing services for reading, writing, and validating XML documents (the most recent release of the parser is called MSXML2—you can stay up-to-date on the latest releases by exploring the following Web site: http://msdn.microsoft.com/downloads/webtechnology/xml/msxml.asp).

Although MSXML certainly is powerful, writing the code to traverse all the nodes in an XML document by using the exposed Document Object Model (DOM) and extracting their contents with a complex schema is not my idea of good time. To make this process seamless to a VB developer who lacks expertise in MSXML, this project used the XML Code Generator that the folks from DevelopMentor posted on MSDN (http://www.develop.com/dm/default.asp). You can download the code generator at http://msdn.microsoft.com/xml/articles/generat.asp. This generator reads the XML schema and creates an ActiveX DLL project with VB class modules that provide an object model for your schema. Each class maps to an element in the schema and is prefaced by CXML_. The generator also creates a CXMLLoad class that exposes a simple LoadXML method to which you can pass the location of the XML file. The method uses the MSXML parser to load the document and populate the object model for your schema. A VB developer can then code against the object collections in order to extract the data and save it into a database without having to traverse the DOM.

For example, when the code generator is run against the schema in Listing 1, it creates one MultiUse class for the top-level element of the schema—in this case, CXML_CirculationData. The code generator then creates PublicNotCreateable classes for each of the elements beneath it, such as CXML_Document, CXML_DocIndex, and so on. After compiling this DLL, you can create new instances of both the CXML_Load object and the top-level object from your schema from within your VB program, and then load the XML.

Dim objCircData as CXML_CirculationData
Dim objLoad as CXMLLoad

Set objCircData = New CXML_CirculationData
Set objLoad = New CXMLLoad

objLoad.LoadXML strFile, objCircData

In the above example, the CXML_CirculationData object is the top-level object of the hierarchy, and the LoadXML method creates collections of objects beneath it. If the document is parsed without error, a developer can access its data using typical object techniques:

Dim varData(6) As Variant
Dim objDoc As CXML_Document

' Set a reference to the document object
Set objDoc = objCircData.Document(1)

' Package the data into an array for processing

With objDoc
    varData(0) = .Name.CompanyName
    varData(1) = .Name.Dept
    varData(2) = .Name.FName
    varData(3) = .Name.LName
    varData(4) = .Name.NameAdd
    varData(5) = .Name.NameChange
    varData(6) = .Name.SubscriberTitle
End With

' Call the MTS component to save the data
objDETx.Save varData  'And pass other data as well

Here, the code packs the attributes of the Name element into a Variant array and loads them into the database using a custom component that runs in MTS.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.