将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文件。
0
投稿
猜你喜欢
- 用户登录验证脚本,Chkpwd.asp<% '=======用户登录验证脚本======= '如果尚未定义Passed
- 由于特定需求,最近实验室需要远程连接外地的sql server 2000服务器,最开始怎么连也连不上,出现了很多问题,但是在今天上午,借用实
- 如何正确理解和使用Command、Connection和 Recordset三个对象?我知道它们都是连接数据库的“好手”,但在编程的具体应用
- 50个常用sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(
- 近日大家热议的盗版XP黑屏问题想必很多人都知道了,在这里就不多说。据媒体报道,微软公司将实施逐步投放策略,预计本周将投放5%黑屏,下周投放2
- 行高的概念看上去很简单——文字行的高度,其实,行高所涉及到的基础知识,对于今后理解其它属性也很重要。大片密密麻麻的文字往往会让人觉得乏味,因
- MySQL的Explain命令用于查看执行效果。虽然这个命令只能搭配select类型语句使用,如果你想查看update,delete类型语句
- 页签的流行自从Yahoo!的首页引进页签(tab, 见下图)之后,这种可用性极佳的方式越来越受欢迎,用户也逐步习惯和喜欢上它,因为它可以在原
- CSS圆角的现实一直是大家所热衷的话题,我们进行CSS布局一直强调语义,强调文档的结构。圆角作为页面的外面表现,应该分离到CSS文件中,可以
- Sitepoint有一个不错的针对Web开发者和设计者的参考手册网站,可以查询HTML、CSS和JavaScript的基本信息、兼容性和示例
- 通过锁机制,可以实现多线程同时对某个表进行操作。如下图所示,在某个时刻,用户甲、用户乙、用户丙可能会同时或者先后(前面一个作业还没有完成)对
- 听说安全地断开Connection连接的记录集可以提高ASP的运行速度,请问如何实现?很多人会将一个Connection对象存储在Appli
- <input name="a" type="checkbox"
- 国外的空间和我们国内的空间使用的语言系统一般不一样,所以在网页程序上时如果处理不当很容易出现乱码,看了让人摸不着头脑。所以我们在编写程序时就
- 关于数据库的逻辑设计,是一个很广泛的问题。本文主要针对开发应用中遇到在MS SQL Server上进行表设计时,对表的主键设计应注意的问题以
- 方案5 使用xml参数 对sql server xml类型参数不熟悉的童鞋需要先了解下XQuery概念,这里简单提下XQuery 是用来从
- Dreamweaver从出道以来,其简便易用而又功能强大的“行为”(Behavior)一直是大家所关注的热点之一。在Dreamweaver
- 上次我重新修改了UBB的转换后,又很多朋友反映日文显示的时候出错了。我在本地测试了一下,结果出现了 Invalid procedure ca
- 这几天不是很忙,就找了些拖动布局方面的资料看看,也学着写了个拖动布局的效果,没想到花了好多时间,七拼八凑,总算是把这个效果写出来了。哎!还是
- 我们可以利用Session对象来进行注册验证。Session对象会帮我们把某一用户的信息保留下来,让后续的网页读取。我们就可以在用户注册成功