Presentation is loading. Please wait.

Presentation is loading. Please wait.

Generating XML Data from a Database Eugenia Fernandez IUPUI.

Similar presentations


Presentation on theme: "Generating XML Data from a Database Eugenia Fernandez IUPUI."— Presentation transcript:

1 Generating XML Data from a Database Eugenia Fernandez IUPUI

2 N-Tier Architecture Data sources available at back end Static XML Document Data Sources (e.g. a database) Middle tier merges data from various data sources and converts the data into a suitable format The client processes and presents data in an appropriate format LCD currently HTML

3 Sending & Presenting Data to Client: The Choices Convert to HTML less browser compatibility problems data loses semantic meaning more work done at server Convert to XML and XSL data retains its semantic meaning processing done locally on client browser must be XML compatible

4 Our Choice: XML The server uses the database schema to construct an XML document. An XSL stylesheet may be attached to format it for the browser. The user fills in the form and sends the revised XML document back to the server. The server converts the XML into appropriate columns in the database.

5 Our Tools: ASP ASP provides server-side scripting. An ASP page consists of text, HTML tags and scripting commands. ASP files have.asp extension and can be edited with any text editor. I recommend using Visual InterDev if you like color coding of elements. For a quick intro to ASP, see my Intro to ASP notes.

6 Our Tools: ADO ADO is a data access object that encapsulates OLE DB. It is the recommended data-access method for Microsoft Enterprise Servers. ADO can be used in ASP pages to query a database and return the results to a Web browser.

7 ADO Database Objects Connection represents the connection to a database source Recordset consists of records returned from a database query Field contains data from single column Fields collection contains all Field objects of a Recordset

8 How ADO Objects Relate Connection Recordset Field Fields Collection Execute or Open Active Connection

9 Connecting to a Data Source 1. Declare a Connection object 2. Create a Recordset object 3. Connect the Recordset to the Connection Object 4. Populate the Recordset by opening it using SQL 5. Reference the fields See the ASP Database Connection notes for details on how to do this.

10 Referencing Fields in a Recordset To use a field in a recordset, you can reference the field name directly reference the field name in the Fields collection use an index to refer to the field (first field has index = 0) Example rsBooks(“title”) rsBooks.Fields(“title”) rsBooks.Fields(0)

11 Navigating a Recordset MethodResult MoveFirstMoves to first record MoveLastMoves to last record MoveNextMoves to next record MovePreviousMoves to previous record MoveMoves a given number of records, forward or backwards recordset.Move NumRecords, [Start] Start can be 0 for current record (the default), 1 for first record, or 2 for last record

12 Checking for Records Use EOF and BOF properties to check if recordset has any records, or if you’ve moved beyond the limits of the recordset BOF = True if current position is BEFORE the first record, False otherwise EOF = True if current position is AFTER the last record, False otherwise If either BOF or EOF is True, there is no current record If you open a Recordset that is empty, both BOF and EOF are set to True

13 Navigation Example No book records found. <% Else Do Until rsBooks.EOF %> Book Title: <% rsBooks(“Title”) rsBooks.MoveNext Loop End If %>

14 Returning XML to the Client <% rsBooks.MoveNext Loop %> must set content type for Response to XML no whitespace between ContentType and XML declaration specify XML root element loop through recordset create XML elements

15 Returning XML as a Data Island <% rsBooks.MoveNext Loop %> must set content type for Response to HTML specify XML DSO ID loop through recordset create XML elements

16 Saving an ADO Recordset Directly as XML An ADO Recordset can be saved directly in XML format rsBooks.save fileName, 1 The persisted recordset consists of a series of elements representing the rows and a schema definition. Simple method, no loop needed output format harder to use

17 Example: Saving ADO as XML <% ‘this example shows how to persist an ADO recordset ‘into a temporary XML file called tempfile.xml ‘Prepare temporary file set fso = CreateObject(“Scripting.FileSystemObject”) fileName = Server.MapPath(“tempfile.xml”) ‘Delete tempfile if it already exists If fso.FileExists(fileName) Then fso.DeleteFile(fileName) End If ‘Save the recordset as XML rsBooks.Save fileName, 1 %>

18 Sources “Building XML-Based Web Applications” a Microsoft Certified Course.


Download ppt "Generating XML Data from a Database Eugenia Fernandez IUPUI."

Similar presentations


Ads by Google