XML and Active Server Pages

Active Server Pages (ASP) is a Microsoft Internet server-side technology 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 db.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 that your code creates an XML document because the default document type is HTML. If the content type item in the HTTP header doesn't indicate that a document is XML, the browser won't treat it as an XML document (and probably will treat it as HTML). You do this in the ASP script with <% Response.ContentType %>, setting the content type header item to "application/xml" this way:

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

I also need to add the XML declaration for the resulting document as well as the document element, <document>:

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

<?xml version="1.0"?>
<document>
    .
    .
    .

Now I need to fetch the names of the students from db.mdb. I'll do this using the Microsoft ADO protocol because ASP is targeted to run on Microsoft platforms such as IIS. I'll create an ADO connection to db.mdb and use an 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:xmldb.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:

<% 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:xmldb.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>

That creates the XML we want. Figure 20.1 shows the results created by this ASP file.

Figure 20.1. Creating XML documents with ASP.


Note that the next few figures look a lot like this one; be sure to watch the title bars for differences.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.94.152