网络编程
位置:首页>> 网络编程>> XML编程>> 将HTML表单数据存储为XML格式(2)

将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
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com