This post contains attachments v20010807155806.zip 
Summary
A brief description of the various ways of using XML as a
Datastore
Introduction
XML is a standardized language for platform independent, machine/human readable data representation which plays a key role in providing and maintaining information not only in the WWW, but also in Enterprise Information Systems, Ecommerce applications/ B2B solutions. The primary reasons are that XML is data centric, XML is easily manipulated and displayed, and XML can be accessed programmatically. XML can be used to both display data and store data.
XML provides us with a variety of ways to access data that is stored in it. I have looked at 3 common ways of doing this. One is using ADO, where the the XML is defined as a ADO datasource and we use VB code to navigate through it just as we would a Recordset object. The second method is using data islands. The third and last method which is using the Microsoft XMLDOM object. The third method is by far the most optimal to access XML data.
XML and ADO
It is possible to read an XML file and write it into a Database and by the same token read a database and display or store it as a XML. The following code loads an XML into a ADODB recordset.
Open a Visual Basic exe module and add a command button to the form and key the following code in
Private Sub Command1_Click()
Dim adoRS As ADODB.Recordset
Set adoRS = New ADODB.Recordset
' Set up the Connection
adoRS.ActiveConnection=Provider=MSDAOSP;
Data Source=MSXML2.DSOControl.2.6;
' Open the XML source
adoRS.Open D:\portfolio.XML
On Error GoTo RecError
printtbl adoRS, 0
GoTo Bye
RecError:
Debug.Print Err.Number & : & Err.Description
If adoRS.State = adStateOpen Then
For Each Col In adoRS.Fields
Debug.Print Col.Name & : & Col.Status ' Error Status
Next Col
End If
Bye:
If adoRS.State = adStateOpen Then
adoRS.Close
End If
Set adoRS = Nothing
End Sub
' Function to recursively retrieve the data
Sub printtbl(rs, indent)
On Error Resume Next
Dim rsChild As ADODB.Recordset
Dim Col As ADODB.Field
While rs.EOF <> True
For Each Col In rs.Fields
If Col.Name <> $Text Then ' $Text to be ignored
If Col.Type <> adChapter Then
' Output the non-chaptered column
Debug.Print Space(indent) & Col.Name & : & Col.Value,
Else
Debug.Print
' Retrieve the Child recordset
Set rsChild = Col.Value
rsChild.MoveFirst
If Err Then MsgBox Error
printtbl rsChild, indent + 4
rsChild.Close
Set rsChild = Nothing
End If
End If
Next
Debug.Print
rs.MoveNext
Wend
End Sub
The following code reads data from a database and saves it as an XML document
Private Sub Command2_Click()
' To read from a database and save the data as XML file
Dim EMPLOYEE As ADODB.Recordset
Dim ObjConn As New ADODB.Connection
ObjConn.Open Nwind
Set EMPLOYEE = ObjConn.Execute(Select employeid, lastname, firstname from Employees)
'This is the code that saves the record set as a XML file
EMPLOYEE.Save d:\crv1\test\XML\employees.XML, adPersistXML
End Sub
Summing up on ADO
As you can see reading a XML data source is analogous to reading a Recordset. This method is particularly useful when the workflow layer components convert Microsoft ActiveX(r) Data Objects (ADO) recordsets they received from the business logic layer into an XML format string before sending the output to the presentation layer. The presentation layer converts the XML data it receives from the workflow layer into HTML for presentation. The XML format simplifies the transformation for presentation because the presentation layer needs to iterate through XML instead of ADO recordsets. It also simplifies caching and state management, because XML data, as a string, can be cached and passed anywhere a string can be cached. Because workflow components serve up XML, they can work with any presentation technology and easily interoperate with other applications.
Please note that ADO recordsets need not be bringing back data from a SQL database they could very well be reading dissimilar databases. Thus using XML greatly simplifies Integration between the various systems within an Enterprise.
Data Islands
Data Islands are simply a way to hold XML code within a HTML page in Microsoft IE5 without using the OBJECT tag. Data Islands expose fundamental recordset properties such as movefirst(), movelast(), addnew(). Data Islands can also be saved on physical storage. There are two types of Data Islands inline and standalone.
In Inline data islands, the XML code is embedded within the HTML page.
The following is an example
<XML ID=XMLID>
<customer>
<name>Joe Black </name>
<custID>90210</custID>
</customer>
</XML>
or referenced through a SRC attribute on the <XML> tag:
<XML ID=XMLID SRC=customer.XML></XML>
The XML ID here is what is used to bind to the XML data.
Defining the data island
Start the data island with the code <XML ID = XMLCustomer>
Open the root node and populate the XML elements
<HTML>
<HEAD></HEAD>
<TITLE> CUSTOMER DETAILS USING DATA ISLANDS </TITLE>
<BODY>
<XML ID = xmlCustomer>
<?xml version = 1.0 ?>
<CUSTOMER DETAILS>
<CUSTOMER>
<NAME> John Doe </NAME>
<TELEPHONE>626-555-5555</TELEPHONE>
<EMAIL>JOHNDOE@COMPANY.COM</EMAIL>
</CUSTOMER>
<CUSTOMER>
<NAME> Jane Doe </NAME>
<TELEPHONE>626-555-1234</TELEPHONE>
<EMAIL>JaneDoe@company.com</EMAIL>
</CUSTOMER>
<CUSTOMER>
<NAME> Joe Black </NAME>
<TELEPHONE>626-666-6666</TELEPHONE>
<EMAIL>JoeBlack@company.com</EMAIL>
</CUSTOMER>
</CUSTOMERDETAILS>
</XML>
Defining the data consumer
<TABLE ID=tblCustomer DATASRC = #xmlCustomer >
<THEAD>
<TR>
<TH> NAME </TH>
<TH> TELEPHONE </TH>
<TH> EMAIL </TH>
</TR>
</THEAD>
<TR>
<TD><DIV datafld=NAME></DIV></TD>
<TD><DIV datafld=TELEPHONE></DIV></TD>
<TD><DIV datafld=EMAIL></DIV></TD>
</TR>
</TABLE>
Summing up on data islands
As is obvious from the example, data islands are purely client side based animals. This means that small volume of data that is static or is almost static can be displayed using data islands. This reduces the number of calls to the server and greatly reduces the load on the network resources.
On the flip side, large amounts of dynamic data cannot be used in data islands as they would load up the client side severely and bring down performance, not to mention the load on the networks.
Using Microsoft DOM object to access XML Data
XML files can be manipulated using VB and ASP either by parsing the XML file using custom string manipulation code or we could use the Microsoft DOM object.
Please note in order for this to work, it is essential that you have Microsoft XML Parser installed on your computer. It can be downloaded at http://www.topxml.com/parsers
This object can be instatiated in the following way in VB
Set XMLdoc = CreateObject(Microsoft.XMLDOM)
Another way of creating the object Set XMLdoc = New MSXML2.DOMDocument30
and in the following way in ASP
<% Dim XMLdoc
Set XMLdoc = CreateObject(Microsoft.XMLDOM)
%>
Once instantiated they can be used to manipulate XML data objects in a variety of ways.
Lets walk thru some examples
Displaying the data in a XML document
To begin with open a form and a add a button to it. Click on the button and add the following code to it
Private Sub Command1_Click()
Dim root
Dim XMLdoc
Dim child
Set XMLdoc = CreateObject(Microsoft.XMLDOM) 'One way of creating the object
Set XMLdoc = New MSXML2.DOMDocument30 'Another way of creating the object
Please ensure that you use only one of the following ways to the Object
XMLdoc.async = False
The above specifies that the ASP processing should wait for the XMLDoc object to finish processing before proceeding to the next line, as opposed to continuing processing while the XMLDoc object does its things.
XMLdoc.Load (D:\portfolio.XML)
Set root = XMLdoc.documentElement ' Point to the root node
For Each child In root.childNodes ' walking thru the child nodes
MsgBox child.Text
Next
End Sub
Writing data into a XML doc
Private Sub Command2_Click()
Dim XMLdoc
Set XMLdoc = CreateObject(Microsoft.XMLDOM)
XMLdoc.async = False
Create the root node
Set XMLroot = XMLdoc.createElement(Customer)
XMLdoc.appendChild (objXMLroot)
XMLroot.setAttribute Name,John Doe
Set XMLChildNode = XMLdoc.createNode(element, Customer Details , )
XMLdoc.documentElement _
.appendChild (XMLChildNode)
Set objPhonenumber = XMLdoc.createNode(element, _
Phone Number, )
objPhonenumber.Text = 626-555-5555
XMLChildNode.appendChild (objPhonenumber)
Set objPhonenumber = Nothing
Set XMLcomment = XMLdoc _
.createComment(This is a comment.)
XMLChildNode.appendChild (XMLcomment)
Set XMLcomment = Nothing
Set XMLcdata = XMLdoc _
.createCDATASection(This is a CData Section.)
XMLChildNode.appendChild (XMLcdata)
Set XMLcdata = Nothing
Set XMLdata = XMLdoc _
.createTextNode(This is a Text Node.)
XMLChildTestNode.appendChild (XMLdata)
Set XMLdata = Nothing
XMLdoc.Save d\PubInfo.XML
MsgBox (file written)
Set XMLdoc = Nothing
End Sub
Here we make use of the various methods and properties associated with the Microsoft DOM object such as
- appendchild which Appends newChild as the last child of the node
- setAttribute which Sets the value of the named attribute.
- CreateComment which Creates a comment node that contains the supplied data
- createCDATAsection which Creates a CDATA section node that contains the supplied data.
- CreateTextNode which Creates a text node that contains the supplied data.
As is obvious, the accessing data using the Microsoft XMLDOM Object seems like the best way to access the XML data object.
Conclusion
We have discussed 3 methods of accessing XML data sources :
1. As Data Islands
2. As Recordset objects
3. As Microsoft DOM Objects
each has its own advantages and shortcomings depending on the context in which it is used . It is up to the programmer to decide which suits his needs best. But, it reiterates the fact that XML is a very flexible, and simple protocol.
|