Convert the data submitted in an HTML form into an XML file

Source: Internet
Author: User

Generally, the data submitted by form forms in ASP is written into the database management system. If you want your data to be easily carried, you can write it into an XML file. This method is cross-platform, so the information you collect does not need to be converted.
To implement the above idea, you must first use Microsoft xmldom to create an XML file. The Microsoft xmldom object has an extended object library, it can create elements, attributes, and attribute values required to form an XML file.
After the xmldom object is instantiated, each element must be referenced to create an XML structure. The following example is to create a root element and append it to the XML file; create sub-elements and append the attribute values of the elements. Save as an XML file.

Instantiate Microsoft xmldom object:CopyCodeThe Code is as follows: <%
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 save the file and open 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>

The following describes how to use this object to store user input information.

first, create the input form page.
entercontact.html: copy Code the code is as follows:

<br> Communication Information <br>



enter your communication information:


Name:
sex:
address:
Work Unit:
Telephone:
Email:



Then write ASP code, traverse the form elements, and write the input information into the XML file.
Processform. asp:Copy codeThe Code is as follows: <%
'--------------------------------------------------------------------
The 'function "convertformtoxml" receives the passed parameters.
'Strxmlfilepath-the physical address of the XML file.
'Strfilename-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 a Microsoft xmldom object.
Set objdom = server. Createobject ("Microsoft. xmldom ")
Objdom. preservewhitespace = true

'Create the contact element of the XML document and append it to the XML document.
Set objroot = objdom. createelement ("Address Book ")
Objdom. appendchild objroot

'Traverses the set of form elements of the request object.
For x = 1 to request. Form. Count

If instr (1, request. Form. Key (x), "BTN") = 0 then

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

'Create property "ID ".
Set objattid = objdom. createattribute ("ID ")

'Set the value of the ID attribute to be equal to the value of each element in form.
Objattid. Text = request. Form. Key (X)

The 'setattributenode method appends the ID attribute value to the field element.
Objfield. setattributenode objattid

'Create the attribute taborder.
Set objatttaborder = objdom. createattribute ("taborder ")

'Set the attribute value of taborder
Objatttaborder. Text = x

'Append the attribute value of taborder to the field element.
Objfield. setattributenode objatttaborder

'Create a new element field_value.
Set objfieldvalue = objdom. createelement ("field_value ")

'Assign a value
Objfieldvalue. Text = request. Form (X)

'Append the field element as the child element of the root element.
Objroot. appendchild objfield

'Append field_value as the sub-element content
Objfield. appendchild objfieldvalue
End if
Next

'Create an XML Declaration
Set objpi = objdom. createprocessinginstruction ("XML", "version = '1. 0' encoding = 'gb2312 '")

'Append the XML life to the XML document
Objdom. insertbefore objpi, objdom. childnodes (0)

'Save the XML file
Objdom. Save strxmlfilepath & "& strfilename

'Release the value 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 when an error occurs
On Error resume next

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

'The user is prompted if an error occurs. Otherwise, the report is successful.
If err. Number <> 0 then
Response. Write ("An error occurred while saving the form value !!! ")
Else
Response. Write ("submitted successfully !! ")
End if
%>

Use ie5 + to open the contact. xml file.

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.