Convert HTML form submission data to XML file

Source: Internet
Author: User
Tags define local html form mail
Typically, the data submitted by Form forms in ASP is written into the database management system, and if you want your data to be portable, you can write it into an XML file. This approach is cross-platform, so the information you collect does not need to be converted.
To achieve this, you must first build an XML file using Microsoft XMLDOM, and the Microsoft Xmldom object has an extended object library that creates the elements, attributes, and property values that are required to make up an XML file.
After the Xmldom object is instantiated, the elements must be referenced to establish the structure of the XML, and the following example is to create the root element and append it to the XML file, and then each child element is created and the attribute value of the element is appended. Finally, save as an XML file.

Instantiation of Microsoft xmldom Object: <%
Dim Objdom
Dim Objroot
Dim ObjChild1
Dim ObjChild2
Dim OBJPI
Set objdom = Server.CreateObject ("Microsoft.XMLDOM")
Set objroot = objdom.createelement ("RootElement")

Objdom.appendchild Objroot
Set objChild1 = objdom.createelement ("ChildElement1")

Objroot.appendchild ObjChild1
Set objChild2 = objdom.createelement ("ChildElement2")

Objroot.appendchild ObjChild2

Set OBJPI = objdom.createprocessinginstruction ("xml", "version= ' 1.0")
Objdom.insertbefore Objpi, objdom.childnodes (0)
Objdom.save "C:\MyXMLDoc.xml"
%>




Once you have saved the file and opened it with ie5+, the style is as follows [of course, the attribute has not been assigned]:
Myxmldoc.xml: <?xml version= "1.0"?>
<rootElement>
<childelement1/>
<childelement2/>
</rootElement>




 

Let's look at how to use this object to store user input.

First, create the page where you entered the form.
Entercontact.html: <title>
Communication information
</title>
<body>
<form action= "processform.asp" method= "POST" >
Name: <input type= "text" id= "name" name= "name" ><br>
Sex: <input type= "text" id= "Sexname" name= "gender" ><br>
Add: <input type= "text" id= "address" name= "addr" ><br>
Work unit: <input type= "text" id= "Company" name= "work unit" ><br>
Tel: <input type= "text" id= "phone" name= "Telephone" ><br>
E-mail: <input type= "text" id= "email" name= "e-mail" ><br>
<input type= "Submit" id= "Btnsub" name= "Btnsub" value= "submitted" ><br>
</form>
</body>



Then write the ASP code, traverse the form element, and write the input information into the XML file.
Processform.asp: <%
'--------------------------------------------------------------------
The ' function ' Convertformtoxml receives the passed arguments.
' Strxmlfilepath-xml the physical address of the store.
' strFileName-the name of the XML file to be saved.
'--------------------------------------------------------------------
Function Convertformtoxml (Strxmlfilepath, strFileName)
' Define local variables.
Dim Objdom
Dim Objroot
Dim Objfield
Dim Objfieldvalue
Dim Objattid
Dim Objatttaborder
Dim OBJPI
Dim x

' Instantiate the Microsoft Xmldom object.
Set objdom = server. CreateObject ("Microsoft.XMLDOM")
Objdom.preservewhitespace = True

' Creates the root element contact of an XML document and appends it to an XML document.
Set objroot = objdom.createelement ("Address Book")
Objdom.appendchild Objroot

' Traverses the collection of the Request object form element.
For x = 1 to Request.Form.Count

If InStr (1,request.form.key (x), "btn") = 0 Then

' Create the Element field '.
Set objfield = objdom.createelement ("field")

' Create the attribute ' ID.
Set Objattid = Objdom.createattribute ("id")

' The value of the set id attribute equals the value of each element in the form.
Objattid.text = Request.Form.Key (x)

The ' Setattributenode method appends the id attribute value to the field element.
Objfield.setattributenode Objattid

' Create a property taborder.
Set Objatttaborder = Objdom.createattribute ("TabOrder")

' Set the property value of the TabOrder
Objatttaborder.text = X

' Appends the attribute value of the taborder to the field element.
Objfield.setattributenode Objatttaborder

' Create a new element, Field_value.
Set Objfieldvalue = objdom.createelement ("Field_value")

' Assign value
Objfieldvalue.text = Request.Form (x)

' Appends a field element to the child element of the root element.
Objroot.appendchild Objfield

' Append Field_value as the content of the child element
Objfield.appendchild Objfieldvalue
End If
Next


' Create an XML declaration
Set OBJPI = objdom.createprocessinginstruction ("xml", "version= ' 1.0 ' encoding= ' gb2312 '")

' Append XML life to an XML document
Objdom.insertbefore Objpi, objdom.childnodes (0)

' Save XML file
Objdom.save Strxmlfilepath & "\" & strFileName

' Releases the values referenced by all objects
Set objdom = Nothing
Set Objroot = Nothing
Set Objfield = Nothing
Set Objfieldvalue = Nothing
Set Objattid = Nothing
Set Objatttaborder = Nothing
Set OBJPI = Nothing
End Function


' Continue execution when an error occurs
On Error Resume Next

' Call function convertformtoxml the file to the specified file directory and file name.
Convertformtoxml "D:\aspexam", "Contact.xml"

' If an error occurs, the user is prompted or the report succeeds.
If Err.Number <> 0 Then
Response.Write ("Save form value error!!! ")
Else
Response.Write ("submitted successfully!!") ")
End If
%>



Open the Contact.xml file with ie5+.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.