Returning XML from an ASP Web page

Source: Internet
Author: User
Tags constant blank page

Profile

Returning XML data from an Active Server pages (ASP) Web page is a common programming requirement. The method used to implement this requirement varies depending on the version of Microsoft Internet Information Services (IIS) that is used to host the ASP application. The step-by-Step guide In this article shows how to return Extensible Markup Language (XML) data from an ASP page with the relevant sample code.

The following example code creates an ASP page that returns the contents of an ActiveX data Objects (ADO) recordset in XML format. This code connects to an instance of the SQL Server PUBS sample database, and it opens an ADO recordset by executing a SELECT query to retrieve data from the Authors table. Then, using the Save feature of ADO and the Write method of the ASP Response object, this recordset is returned to the client browser in XML format.

1. Prerequisites

The following is a brief list of recommended hardware, software, network architectures, and required Service packs:

Microsoft Windows 2000 Professional, Windows Server, or Windows Advanced server

Microsoft Internet Information Server (IIS) 5.0

Or

Microsoft Windows NT 4.0 Server

Microsoft Internet Information Server (IIS) 4.0

2. Prepare the Web site

In Windows Explorer, create a folder named Xmltest in the root folder of the Web server (usually C:inetpubwwwroot).

Right-click the newly created folder, and then click Properties.

On the Security tab, add the Everyone group and grant Read and write permissions to the Everyone group for this folder. Click OK to accept the change.

On the Start menu, point to the program, point to Administrative Tools, and then click Internet Services Manager.

Under Internet Information Services, double-click to expand the entries that correspond to the local server.

Right-click Default Web site, point to New, and then click Virtual Directory. In the wizard, follow these steps:

When prompted, type Xmltest in the virtual directory alias text box, and then click Next.

When you are prompted to type the content directory for your Web site, click Browse, select the newly created Xmltest directory, and then click Next.

When you are prompted to select access rights, select Read and run scripts (for example, ASP). No additional access rights are required in this example. Click Next to complete the wizard.

Double-click Default Web site.

Right-click the new virtual directory, and then click Properties.

On the Table of Contents tab, check whether the name of the Web site (the name you typed in step 6 a) is listed in the Application Name text box below the application settings. If it is not listed, click Create to create the application.

Closes the Properties dialog box and IIS.

3. Windows XML Sample Code

On the Start menu, point to the program, point to the attachment, and then click Notepad.

Select the following code, right-click the selection, and then click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file:

<%

' Very Important:set the ContentType property of the Response object to Text/xml.

Response.ContentType = "Text/xml"

Dim cn

Dim RS

Dim xmldoc

Set cn=server.createobject ("ADODB. Connection ")

Set rs=server.createobject ("ADODB. Recordset ")

' Replace the ADO Connection string attributes

' In the following line of code to point to your

' Instance of SQL Server, and to specify the

' Required security credentials for User ID and Password.

cn. Open "Provider=SQLOLEDB.1;" & _

"User id=;" & _

"Password=;" & _

"Initial catalog=pubs;" & _

"Data source="

Rs. CursorLocation = 3

Rs. Open "Select * from Authors", CN

' Persist the Recorset in XML format to the ASP Response object.

' The constant value for adPersistXML is 1.

Rs. Save Response, 1

%>

In the 20th line of code, you will be replaced with your user name.

In the 21st line of code, you will be replaced with your password.

In the 23rd line of code, you will be replaced with your SQL Server.

On the File menu, click Save.

In the Drop-down list box, browse to the Xmltest folder that you created earlier. In the File Name text box, type xmlw2k.asp and click All files in the Save Type Drop-down box. Last click Save to save the file.

To view the page, start your Web browser, and then type the HTTP location of the page in the Address bar. If you saved the file to the location mentioned earlier, type the http://< server name >/xmltest/xmlw2k.asp in the Address bar.

4. Windows NT 4.0 XML sample code

On the Start menu, point to the program, point to the attachment, and then click Notepad.

Select the following code, right-click the selection, and then click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file:

<%

' Very Important:set the ContentType property of

' The Response object to Text/xml.

Response.ContentType = "Text/xml"

Dim cn

Dim RS

Dim xmldoc

Set cn=server.createobject ("ADODB. Connection ")

Set rs=server.createobject ("ADODB. Recordset ")

' Replace the ADO Connection string attributes

' In the following line of code to point to your

' Instance of SQL Server, and to specify the

' Required security credentials for User ID and Password.

cn. Open "Provider=SQLOLEDB.1;" & _

"User id=;" & _

"Password=

"Initial catalog=pubs;" & _

"Data source="

Rs. CursorLocation = 3

Rs. Open "Select * from Authors", CN

Set xmldoc = Server.CreateObject ("Microsoft.XMLDOM")

' Persist the Recorset in XML format to the DOMDocument object.

' The constant value for adPersistXML is 1.

Rs. Save xmldoc,1

Rs. Close

cn. Close

Set rs = Nothing

Set cn = Nothing

' Write out the xml ' the DOMDocument

' Object to the ' client Browser

Response.Write Xmldoc.xml

%>

In the 20th line of code, you will be replaced with your user name.

In the 21st line of code, you will be replaced with your password.

In the 23rd line of code, you will be replaced with your SQL Server.

On the File menu, click Save.

In the Drop-down list box, browse to the Xmltest folder that you created earlier. In the File Name text box, type xmlnt4.asp and click All files in the Save Type Drop-down box. Last click Save to save the file.

To view the page, start your Web browser, and then type the HTTP location of the page in the Address bar. If you saved the file to the location mentioned earlier, type the http://< server name >/xmltest/xmlnt4.asp in the Address bar. Note: This Windows NT 4.0 sample code can also be run when IIS 5.0 is used in Windows 2000.

5. Defects

When you visit an ASP page, the browser returns a blank page. Please check that you include the following line at the top of the ASP page:

Response.ContentType = "Text/xml"

This should be the first line of code in an ASP page

In IIS 5.0 (ASP 3.0), the ASP Response object implements the IStream interface. So you can keep an ADO recordset (XML format), or a MSXML DOMDocument object loaded with XML data, directly to the Response object.

In IIS 4.0 (ASP 2.0), the ASP Response object does not implement the IStream COM interface. Therefore, you must use the Write method of the Response object to return XML from the ASP. The argument passed to the Write method must be a fully formatted XML string, or a variable that contains a fully formatted XML string.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.