将HTML表单数据存储为XML格式(2)
作者:Dicky 发布时间:2007-08-23 13:04:00
标签:xml,表单
将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文件。


猜你喜欢
- 目录1 figure1.1 创建figure1.2 figure的常用设置1.2.1 set方法通用设置1.2.2 设置figure标题1.
- 前言最近在重构一个复选框组件,原型是select2这个jQuery插件, 有兴趣的可以去搜下,封装的很好,但是并不能满足业务所有需求,最要命
- 问题mp加密与druid和nacos结合,首次项目启动成功,后续访问无法连接数据库导致原因项目首次加载由于会去nacos读取一遍配置,刚好m
- 如:获得 2015-03-01 日的前7天的日期 : select date_sub('2015-03-01',interv
- 正在看的ORACLE教程是:Oracle Index 的三个问题。索引( Index )是常见的数据库对象,它的设置好坏
- run() 方法并不启动一个新线程,就是在主线程中调用了一个普通函数而已。start() 方法是启动一个子线程,线程名就是自己定义的name
- odeJs 微信公众号功能开发,移动端 H5页面调用微信的支付功能。这几天根据公司的需要使用 node 和 h5页面调用微信的支付功能完成支
- 最近几天仔细研究了一下vertical-align这个属性,结果让我大吃一惊,这个很“资深”的CSS标准竟然在各个浏览器里面的表现都各不相同
- 本文实例讲述了JS实现匀速与减速缓慢运动的动画效果。分享给大家供大家参考,具体如下:<!DOCTYPE html><htm
- 最近开始学习数据库知识,从mysql下手,下面详细介绍一下安装过程,给小伙伴们一个参考。一、安装 首先,从mysql的中文社区下载,我尝试过
- from sgmllib import SGMLParserimport urllib2class sgm(SGMLParser):&nbs
- 工欲善其事,必先利其器.python是解释型的语言,但是在windows下如果要执行程序的话还得加个python shell的话,
- 首先,你得下载SocksiPy这个.解压出来之后里面会有一个socks.py文件.然后你可以把这个文件复制到python安装目录里面的Lib
- 解决SQL Server 连接失败的问题最近因学习数据库系统原理,下载安装了一个2019版本的,启动服务后,发现使用Aqua Data St
- 最近对爬虫比较感兴趣,所以就学了一下,看人家都在网上爬取那么多美女图片养眼,我也迫不及待的试了一下,不多说,切入正题。其实爬取图片和你下载图
- 由于网络带宽以及某些WAP服务器DECK传输的限制,所以DECK越小越好,最好不要超过1.2K。如果你的需求很复杂,最好分成几个DECK来完
- JavaScript 中的并没有提供像 VBScript 里的 DateAdd 方法用于日
- 一、前言最近经常碰到开发误删除误更新数据,这不,他们又给我找了个麻烦,我们来看下整个过程。二、过程由于开发需要在生产环节中修复数据,需要执行
- 装饰器对与Python新手以至于熟悉Python的人都是一个难理解, 难写的东西. 那么今天就分享一下我对Python 装饰器的理解所谓装饰
- 根据 Dotzler 的统计,IE6 的份额正在缩水,这可能是 2009 年本人听到的第一个好消息。于此同时,Gmail 的浏览器支持列表中