如你熟知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文件。