XML

Working With Streams

In the above examples, we have been saving information to data files. It's likely that you will not want to do this for most of your applications. Instead, you can work directly with data streams that can pass the data into a DOM object or pass it back to the client in an ASP Response object. Now we'll look at an example that shows how we can use a stream to place the data directly into the DOM objects.

This example will load data from a text file and place it into an ADO Stream object. The data will then be loaded into a DOM object. Once you have the data in the DOM object, you can do almost anything you want to it, including transforming it with XSL. To place the data into an ADO Stream object, add another command button to the form. Name it cmdStream with a caption Stream and add the following code to the click event handler of the command button cmdStream:

  Private Sub cmdStream _Click()
      Dim objNWRecordset As ADODB.Recordset
      Dim objADOStream As ADODB.Stream
      Dim objDOM As DOMDocument
      Dim strXML As String
      Dim objNWConnection As ADODB.Connection
      Set objADOStream = New ADODB.Stream
      Set objDOM = New DOMDocument
      Set objNWRecordset = New ADODB.Recordset
      Set objNWConnection = New ADODB.Connection
      objNWConnection.CursorLocation = adUseClient
      'You will need to replace IES-FUJI with the appropriate data
      'source in the following statement.
      objNWConnection.Open _
      "Provider=SQLOLEDB.1;Integrated Security=SSPI;" & _
      "Persist Security Info=False;" & _
      "User ID=sa;Initial Catalog=Northwind;Data Source=IES-FUJI"
      objNWRecordset.CursorLocation = adUseClient
      objNWRecordset.CursorType = adOpenStatic
      Set objNWRecordset.ActiveConnection = objNWConnection
      objNWRecordset.Open "Products"
      objADOStream.Open
      objNWRecordset.Save objADOStream, adPersistXML
      strXML = objADOStream.ReadText
      objDOM.loadXML strXML
  End Sub

Just as in our other examples, this code creates an ADO Connection object and a Recordset object. Once the connection to the Northwind Traders database has been made and the recordset contains the data from the Products table, the XML data is saved to an ADO Stream object called objADOStream. The information is passed from the ADO Stream object to a string variable called strXML, and then the XML data is placed into a DOM document object called objDOM using the loadXML method of the document object. In this way, we have opened data from a regular database and passed the data as XML into a DOM document object.

As you can see, the ADO recordset is versatile and can be used on both the client and the server to build powerful data access components in distributed applications.

by BrainBellupdated
Advertisement: