选择显示字体大小

从 asp web 页返回 xml


  概要
从 active server pages (aspweb 页返回 xml 数据是一种常见的编程要求。 实现此要求所用的方法因用来托管 asp 应用程序的 microsoft inte.net 信息服务 (iis) 的版本而异。  本文中的分步指南带有相关的示例代码,演示了怎样从 asp 页返回可扩展标记语言 (xml) 数据。 

下面的示例代码创建一个 asp 页,此页返回xml 格式的 activex data objects (ado) 记录集的内容。 此代码连接着 sql server pubs 示例数据库的一个实例,而且它通过执行 select 查询语句以从 authors 表中检索数据的这一方式来打开 ado 记录集。 然后,使用 ado 的保存功能和 asp response 对象的 write 方法,将此记录集以 xml 的格式返回给客户端浏览器。
1. 先决条件
下面简要列出了推荐使用的硬件、软件、网络架构以及所需的 service pack: 
microsoft windows 2000 专业版、windows 2000 server 或 windows 2000 advanced server 
microsoft inte.net information server (iis) 5.0 



microsoft windows nt 4.0 server 
microsoft inte.net information server (iis) 4.0 
2. 准备 web 站点
在 windows 资源管理器中,在 web 服务器的根文件夹(通常在 c:\.netpub\wwwroot\)下创建一个名为 xmltest 的文件夹。 
右键单击新创建的文件夹,然后单击属性。 
安全选项卡上,添加所有人组,并向“所有人”组授予对此文件夹的读和写权限。单击确定接受更改。
在开始菜单上,指向程序,指向管理工具,然后单击 inte.net 服务管理器。 
在 inte.net 信息服务下,双击展开对应于本地服务器的条目。 
右键单击“默认 web 站点”,指向新建,然后单击虚拟目录。 在向导中,按照下列步骤操作: 
系统提示时,在虚拟目录别名文本框中键入 xmltest,然后单击下一步。 
当提示您键入 web 站点内容目录时,单击浏览,选择新创建的 xmltest 目录,然后单击下一步。 
在提示您选择访问权限时,选择读和运行脚本(例如 asp)。 本例中不需要其他任何访问权限。单击下一步以完成向导。 
双击“默认 web 站点”。
右键单击新的虚拟目录,然后单击属性。
在目录选项卡上,检查在应用程序设置下面的应用程序名文本框中是否列出了 web 站点名称(在步骤 6a 中键入的名称)。如果未列出,请单击创建以创建该应用程序。 
关闭属性对话框和 iis。 
3. windows 2000 xml 示例代码
在开始菜单上,指向程序,指向附件,然后单击记事本。
选定以下代码,右键单击所选内容,然后单击复制。在记事本中,单击编辑菜单上的粘贴,将以下代码添加到该文件中: 
<% 

'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=<userid>;" & _
        "password=<password>;" & _
"initial catalog=pubs;" & _
        "data source=<servername>"

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

%>
在第 20 行代码中,将 <userid> 替换为您的用户名。
在第 21 行代码中,将 <password> 替换为您的密码。
在第 23 行代码中,将 <servername> 替换为您的 sql server。
在文件菜单上,单击保存。
在保存在下拉列表框中,浏览到您在前面创建的 xmltest 文件夹。 在文件名文本框中,键入 xmlw2k.asp,并在保存类型下拉框中单击所有文件。最后单击保存以保存该文件。
若要查看该页,请启动 web 浏览器,然后在地址栏中键入该页的 http 位置。 如果您将文件保存到了前面提到的位置,则请在地址栏中键入 http://< 服务器名 >/xmltest/xmlw2k.asp 。 
4. windows nt 4.0 xml 示例代码
在开始菜单上,指向程序,指向附件,然后单击记事本。
选定以下代码,右键单击所选内容,然后单击复制。在记事本中,单击编辑菜单上的粘贴,将以下代码添加到该文件中: 
<% 
'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=<userid>;" & _
        "password=<password;" & _
"initial catalog=pubs;" & _
        "data source=<servername>"

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 property of the domdocument
'object to the client browser
response.write xmldoc.xml
%>
在第 20 行代码中,将 <userid> 替换为您的用户名。
在第 21 行代码中,将 <password> 替换为您的密码。
在第 23 行代码中,将 <servername> 替换为您的 sql server。
在文件菜单上,单击保存。
在保存在下拉列表框中,浏览到您在前面创建的 xmltest 文件夹。 在文件名文本框中,键入 xmlnt4.asp,并在保存类型下拉框中单击所有文件。最后单击保存以保存该文件。
若要查看该页,请启动 web 浏览器,然后在地址栏中键入该页的 http 位置。 如果您将文件保存到了前面提到的位置,则请在地址栏中键入 http://< 服务器名 >/xmltest/xmlnt4.asp。备注: 当在 windows 2000 中使用 iis 5.0 时,此 windows nt 4.0 示例代码也可以运行。
5. 缺陷
当您访问 asp 页时,浏览器返回空白页。 请检查您是否在 asp 页的顶部包括了下面这一行: 
response.contenttype = "text/xml"
这应是 asp 页中的第一行代码 
在 iis 5.0 (asp 3.0) 中,asp response 对象实现了 istream 接口。 所以可以将一个 ado 记录集(xml 格式),或一个加载了 xml 数据的 msxml domdocument 对象直接保持到 response 对象。 
在 iis 4.0 (asp 2.0) 中,asp response 对象未实现 istream com 接口。 所以必须使用 response 对象的 write 方法从 asp 中返回 xml。 传递给 write 方法的参数必须是一个完整格式的 xml 字符串,或是包含一个完整格式 xml 字符串的变量。 


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons