XML and ASP

ASP is one of Microsoft's Internet server-side technologies that lets you create Web documents on the fly. It runs on servers such as the Microsoft Internet Information Server (IIS). In this case, I'll search ch20_01.mdb for the names of the students and return them in an XML document like this, using <document> as the document element and <student> for each student:

 <?xml version="1.0"?>  <document>     <student>         Ann     </student>     <student>         Mark     </student>     <student>         Ed     </student>     <student>         Frank     </student>     <student>         Ted     </student>     <student>         Mabel     </student>     <student>         Ralph     </student>     <student>         Tom     </student> </document> 

The main trick in the .asp file is to make sure your code creates an XML document because the default document type is HTML. If you don't make sure that the content type item in the HTTP header indicates that a document is XML, the browser isn't going to treat it as an XML document (and will probably treat it as HTML). The way you do this in the ASP script is with <% Response.ContentType %> , setting the content type header item to "application/xml" this way:

 <% Response.ContentType = "application/xml" %>      .     .     . 

I also add the XML declaration for the resulting document, and the document element, <document> :

 <% Response.ContentType = "application/xml" %>  <?xml version="1.0"?>   <document>  .      .      . 

Now I've got to fetch the names of the students from ch20_01.mdb. I'll do this using Microsoft ADO since ASP is targeted to run on Microsoft platforms like IIS. I create an ADO connection to ch20_01.mdb and use a SQL statement to return a record set of all records:

 <% Response.ContentType = "application/xml" %>  <?xml version="1.0"?> <document>  <%   DIM adoConnect   DIM adoRecordset   Set adoConnect = Server.CreateObject("ADODB.Connection")   adoConnect.open "Provider=Microsoft.Jet.OLEDB.4.0;" _   & "Data Source=C:\xml\ch20_01.mdb"   Set adoRecordset = adoConnect.Execute("SELECT * FROM Students")  .     .     . 

All that's left is to loop over each record and create the corresponding XML <student> element:

Listing ch20_02.asp
 <% Response.ContentType = "application/xml" %> <?xml version="1.0"?> <document> <% DIM adoConnect DIM adoRecordset Set adoConnect = Server.CreateObject("ADODB.Connection") adoConnect.open "Provider=Microsoft.Jet.OLEDB.4.0;" _     & "Data Source=C:\xml\ch20_01.mdb" Set adoRecordset = adoConnect.Execute("SELECT * FROM Students")  Do While Not adoRecordset.EOF   Response.Write "<student>" + adoRecordset("Name") + "</student>"   adoRecordset.MoveNext   Loop   adoRecordset.Close   set adoRecordset = Nothing   %>   </document>  

And that creates the XML we want. You can see that result, as created by this ASP file, in Figure 20-1.

Figure 20-1. Creating XML documents with ASP.

graphics/20fig01.gif



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net