Store HTML form data in XML format (2)

Source: Internet
Author: User
Tags format object end html form key mail processing instruction version
xml| data

Once the document is archived, if you open the document again, it will appear as a list of the following code:

Myxmldoc.xml:

<?xml version= "1.0"?>
<rootElement>
<childelement1/>
<childelement2/>
</rootElement>

In the "Myxmldoc.xml" document, ChildElement1 and ChildElement2 appear in the form of an empty elements. If they are assigned, each value is enclosed by a tag.

Now, let's think about how to write HTML data into an XML document. We already know how to create and store XML documents. The process of writing a form data into an XML document has now evolved into the request Object "s" Form collection and the steps to set each form field's value book to XML element value are repeated. The above can be done through ASP.

Example: Transferring data to XML

Now, let's give an example of an ordinary HTML form. This form has user name, address, telephone, and e-mail and so on several domains. and write this information to the XML file and save it.

Entercontact.html:
<title>
Contact information
</title>
<body>
<form action= "processform.asp" method= "POST" >

Name: <input type= "text" id= "FirstName" name= "FirstName" ><br>
Last Name: <input type= "text" id= "LastName" name= "LastName" ><br>
Address #1: <input type= "text" id= "Address1" name= "Address1" ><br>
Address #2: <input type= "text" id= "Address2" name= "Address2" ><br>
Phone Number: <input type= "text" id= "phone" name= "Phone" ><br>
E-mail: <input type= "text" id= "email" name= "email" ><br>
<input type= "Submit" id= "Btnsub" name= "Btnsub" value= "Submit" ><br>
</form>
</body>

Send the data in form to the processform.asp ... This is an ASP page that will repeatedly call the same function to write form data to an XML file in this ASP.

Processform.asp:

<%
"--------------------------------------------------------------------
"The" Convertformtoxml "Function accepts to parameters.
"Strxmlfilepath-the physical path where the XML file would be saved.
"Strfilename-the name of the XML file that'll 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 the Request Object.

For x = 1 to Request.Form.Count

"Check to" if "btn" are in the name of the form element.
"If It is, then it are 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 name of

"The current form field.

Objattid.text = Request.Form.Key (x)

"The Setattributenode method would 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 ("Field_value")

"Set the value of the Field_value element equal to
"The value of the ' current field ' Form Collection.

Objfieldvalue.text = Request.Form (x)

"Append" is 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

' does not break ' on ' an error.

On Error Resume Next

Called the Convertformtoxml function, passing in the physical path to
"Save the file to and the name," you wish to use for the file.

Convertformtoxml "C:", "Contact.xml"

"Test to" if a error occurred, if so, let the user know.
"Otherwise, tell the user," 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.")
End If
%>



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.