电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> ASP
将HTML表单数据存储为XML格式-ASP教程,XML相关
作者:网友供稿 点击:7
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
  
如你熟知asp,xml和html4。0,请读下列示例
将表单数据存为xml格式
  通常的,asp中表单提交的数据一般被写入数据库。然而,如果你想让发送数据更为简便易行,那么,可以将它书写为xml文件格式。这种方式对于在web上收集的数据更为有用。因为xml对于所用平台来说非常的简便,所以用不着转换数据格式。
  将提交的数据写为xml文档,则需要通过microsoft xmldom object创建一个新的xml文档。microsoft xmldom object拥有一个可扩展对象库,通过它可以创建elements,attributes以及values,通过创建的这些项目则可以组成xml文档。我无法将整个目标模型做个完整的介绍,因为它所包含的内容太广泛,对于将建成的网站来说,目标模型甚至通过自身也能组建一个相对完整的部份。
  在xmldom object被创建出来之后,通过创建目标(此目标是关于组成xml文档中每一层的elements而言)xml的结构会被演示出来。接下来,会举例说明xmldom是怎样被创建出来的。创建root element之后,将它附加在xmldom文件上。然后创建child elements并附加在root element上,最后存储文档。

演示microsoft xmldom 对象 

<% 
dim objdom 
dim objroot 
dim objchild1 
dim objchild2 
dim objpi 
 
" xmldom 对象使用server对象的createobject方法创建 
set objdom = server.createobject("microsoft.xmldom") 
"使用xmldom的createelemnet方法创建一个ixmldomelement对象。 
"createelemnet方法又一个string参数,这个string 表示该element的名称。 
返回值被传递到objroot变量。objroot表示xml文档的根元素.。 
 
set objroot = objdom.createelement("rootelement") 
 
"use the appendchild method of the xmldom object to add the objroot 
"element reference to the xml document. 
 
objdom.appendchild objroot 
 
"now, following the same steps, you will create references to the 
"child elements for the xml document. the only difference is, when the 
"child elements are appended to the document, you will call the 
"appendchild method of the ixmldomelement object rather than the 
"appendchild method of the xmldom object. by using the ixmldomelement 
"to append the children, you are differentiating (and applying tiered 
"structure to) the child elements from the root element. 
 
set objchild1 = objdom.createelement("childelement1") 
objroot.appendchild objchild1 
set objchild1 = objdom.createelement("childelement2") 
objroot.appendchild objchild2 
 
"the final step to take care of before saving this document is to add 
"an xml processing instruction. this is necessary so that xml parsers 
"will recognize this document as an xml document. 
 
set objpi = objdom.createprocessinginstruction("xml","vertsion="1.0"") 
 
"call the insertbefore method of the xmldom object in order to insert 
"the processing instruction before the root element (the zero element 
"in the xmldom childnodes collection). 
 
objdom.insertbefore objpi, objdom.childnodes(0) 
 
"calling the save method of the xmldom object will save this xml 
"document to your disk drive. in this case, the document will be saved 
"to the "c:" drive and will be named "myxmldoc.xml". when saving an 
"xml document, if the file does not exist, it will be created. if it 
"does exist, it will be overwritten. 
 
objdom.save "c:myxmldoc.xml" 
 
%> 

文档被存档之后,如果你再打开这个文档,那么则会以如下代码列表形式出现: 

myxmldoc.xml: 


<?xml version="1.0"?> 
<rootelement> 
<childelement1 /> 
<childelement2 /> 
</rootelement> 

  在"myxmldoc.xml"文档中,childelement1 和 childelement2 会以空的elements形式出现。如果它们被赋值,那么每个值都将由标记符括起来。 
  现在,让我们来思考一下如何将html数据写到xml文档中去。我们已经知道该如何创建和存储xml文档。将一个表单数据写到xml文档中去的过程,现在已演变成为request object"s form collection以及将每一个表单域的value书定到xml element value 中去的步骤重复。以上可以通过asp来完成。
例:将数据输送到xml
  现在,我们举一个普通的html表单的例子来说明。此form有用户名,地址,电话,以及e-mail等几个域。并将这些信息写入xml文件中并保存。 

entercontact.html: 

<html> 
<head> 
<title> 
contact information 
</title> 
</head> 
<body> 
<table border="1"> 
<form action="processform.asp" method="post"> 
<tr><td colspan="2">please input your contact infomation here:</td></tr> 
<tr><td>first name:</td><td><input type="text" id="firstname" name="firstname"></td></tr> 
<tr><td>last name: </td><td><input type="text" id="lastname" name="lastname"></td></tr> 
<tr><td>address #1: </td><td><input type="text" id="address1" name="address1"></td></tr> 
<tr><td>address #2: </td><td><input type="text" id="address2" name="address2"></td></tr> 
<tr><td>phone number: </td><td><input type="text" id="phone" name="phone"></td></tr> 
<tr><td>e-mail: </td><td><input type="text" id="email" name="email"></td></tr> 
<tr><td colspan="2" align="center"><input type="submit" id="btnsub" name="btnsub" value="submit"></td></tr> 
</form> 
</table> 
</body> 
</html> 

  将form 中数据发送到processform.asp.。这是一个asp页面,在这个asp中将反复调用同一个函数将form数据写入xml 
文件。 

processform.asp: 


<% 
’-------------------------------------------------------------------- 
’the "convertformtoxml" function accepts to parameters. 
’strxmlfilepath - the physical path where the xml file will be saved. 
’strfilename - the name of the xml file that will be saved. 
’-------------------------------------------------------------------- 
function convertformtoxml(strxmlfilepath, strfilename) 
’declare local variables. 
    dim objdom 
    dim objroot 
    dim objfield 
    dim objfieldvalue 
    dim objattid 
    dim objatttaborder 
    dim objpi 
    dim x 
    ’instantiate the microsoft xmldom. 
    set objdom = server.createobject("microsoft.xmldom") 
    objdom.preservewhitespace = true 
    ’create your root element and append it to the xml document. 
    set objroot = objdom.createelement("contact") 
    objdom.appendchild objroot 
    ’iterate through the form collection of the request object. 
    for x = 1 to request.form.count 
    ’check to see if "btn" is in the name of the form element. 
    ’if it is, then it is a button and we do not want to add it 
    ’to the xml document. 
        if instr(1,request.form.key(x),"btn") = 0 then 
        ’create an element, "field". 
            set objfield = objdom.createelement("field") 
            ’create an attribute, "id". 
            set objattid = objdom.createattribute("id") 
            ’set the value of the id attribute equal the the name of 
            ’the current form field. 
            objattid.text = request.form.key(x) 
            ’the setattributenode method will append the id attribute 
            ’to the field element. 
            objfield.setattributenode objattid 
            ’create another attribute, "taborder". this just orders the 
            ’elements. 
            set objatttaborder = objdom.createattribute("taborder") 
            ’set the value of the taborder attribute. 
            objatttaborder.text = x 
            ’append the taborder attribute to the field element. 
            objfield.setattributenode objatttaborder 
            ’create a new element, "field_value". 
            set objfieldvalue = objdom.createelement("fieldvalue") 
            ’set the value of the field_value element equal to 
            ’the value of the current field in the form collection. 
            objfieldvalue.text = request.form(x) 
            ’append the field element as a child of the root element. 
            objroot.appendchild objfield 
            ’append the field_value element as a child of the field elemnt. 
            objfield.appendchild objfieldvalue 
        end if 
    next 
    ’create the xml processing instruction. 
    set objpi = objdom.createprocessinginstruction("xml", "version=""1.0""") 
    ’append the processing instruction to the xml document. 
    objdom.insertbefore objpi, objdom.childnodes(0) 
    ’save the xml document. 
    objdom.save strxmlfilepath & "" & strfilename 
    ’release all of your object references. 
    set objdom = nothing 
    set objroot = nothing 
    set objfield = nothing 
    set objfieldvalue = nothing 
    set objattid = nothing 
    set objatttaborder = nothing 
    set objpi = nothing 
end function 
’do not break on an error. 
on error resume next 
’call the convertformtoxml function, passing in the physical path to 
’save the file to and the name that you wish to use for the file. 
convertformtoxml "f:\asp\","contact.xml" 
’test to see if an error occurred, if so, let the user know. 
’otherwise, tell the user that the operation was successful. 
if err.number <> 0 then 
    response.write("errors occurred while saving your form submission.") 
else 
    response.write("your form submission has been saved.<br><a href=’javascript:history.go(-1)’>go back</a>") 
end if 
%> 

  如果你是在你自己的应用程序中使用以上代码,请谨记一件事情,在"convertformtoxml"函数已经运行的情况下,如果xml文件名已经存在,那么,文件将会被覆盖。在此,我建议在使用"convertformtoxml"功能前,最好用随机建立的文件名。这样,就将有价值数据被改写的风险降为零。 
  关于xml文件的产生,举例如下:

contact.xml: 

<?xml version="1.0"?> 
<!-- edited with xmlspy v2004 rel. 4 u (http://www.xmlspy.com) by dicky (apple’s eden) --> 
<contact> 
    <field id="firstname" taborder="1"> 
        <fieldvalue>dicky</fieldvalue> 
    </field> 
    <field id="lastname" taborder="2"> 
        <fieldvalue>gu</fieldvalue> 
    </field> 
    <field id="address1" taborder="3"> 
        <fieldvalue>shanghai</fieldvalue> 
    </field> 
    <field id="address2" taborder="4"> 
        <fieldvalue>beijing</fieldvalue> 
    </field> 
    <field id="phone" taborder="5"> 
        <fieldvalue>123456</fieldvalue> 
    </field> 
    <field id="email" taborder="6"> 
        <fieldvalue>applebbs@gmail.com</fieldvalue> 
    </field> 
</contact> 

  我在此建议:将以上代码复制到你个人网站服务器上的同名页面上,并运行以上示例时。请一定要明确你使用的是对你个人服务器有效的路径和文件名。 
  当你一切准备好时,请再次检验你的xml文件。
文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·如何使XP的目录属性出现"安全"选项-ASP教程,系统相关
·创建有个性的对话框之MFC篇(二)-ASP教程,系统相关
·用InstallShield打包ASP程序-ASP教程,ASP应用
·windows server 2003 中 SQL Server 2000 分布式事务 错误解决方法-ASP教程,系统相关
·创建有个性的对话框之MFC篇(一)-ASP教程,系统相关
·DevExpress打印相关代码-ASP教程,打印相关
·File文件控件,选中文件(图片,flash,视频)即立即预览显示-ASP教程,组件开发
·用Windows的文件映射机制,实现大批量数据的快速存储-ASP教程,系统相关
·ADO如何取得数据库中表的字段信息之一
·使用DEVEXPRESS部件打印时标题的处理-ASP教程,打印相关

最新文章
· SQL注入天书 - ASP注入漏洞全接触
·用.net 处理xmlHttp发送异步请求
·asp.net创建文件夹的IO类的问题
·如何实现ASP.NET网站个性化
·关于ASP.NET调用JavaScript的实现
·ASP利用Google实现在线翻译功能
·Asp无组件生成缩略图
·由HTTP 500 Internal server error想到的...
·实例讲解asp抓取网上房产信息
·改mdb为asp所带来的灾难


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号