Store HTML form data in XML format

Source: Internet
Author: User
Tags add format object end html form key processing instruction version
xml| data as you are familiar with Asp,xml and HTML4. 0, please read the following example

Save form data in XML format

Typically, data submitted by a form in an ASP is typically written to the database. However, if you want to make it easier to send data, you can write it as an XML file format. This approach is more useful for data collected on the Web. Because XML is very simple for the platform you use, you don't need to convert the data format.

To write the submitted data as an XML document, you need to create a new XML document from Microsoft Xmldom object. Microsoft Xmldom object has a library of extensible objects that enables you to create elements,attributes and values that can be made into an XML document by creating these items. I can't make a complete presentation of the entire target model because it contains too much content, and for the site to be built, the target model can even form a relatively complete part of itself.

After Xmldom object is created, the structure of the XML is demonstrated by creating a goal, which is about the elements that makes up each layer of the XML document. Next, give an example of how XMLDOM was created. After you create the root element, attach it to the xmldom file. The child elements is then created and appended to the root element, and the document is finally stored.


Demo Microsoft Xmldom objects

<%
Dim Objdom
Dim Objroot
Dim ObjChild1
Dim ObjChild2
Dim OBJPI

The Xmldom object is created using the CreateObject method of the Server object
Set objdom = Server.CreateObject ("Microsoft.XMLDOM")
"Creates a IXMLDOMElement object using the Xmldom Createelemnet method.
The Createelemnet method has another string parameter, which represents the name of the element.
The return value is passed to the Objroot variable. Objroot represents the root element of an XML document ...

Set objroot = objdom.createelement ("RootElement")

"Use the AppendChild method of the XMLDOM Object to add the Objroot
The Element Reference to the XML Document.

Objdom.appendchild Objroot

"Now, following the same steps, you'll create references to the
"Child elements for the XML Document." The only difference are, when the
"Child elements are appended to the document, you'll 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, your are differentiating (and applying tiered
"Structure to" is 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 are to add
"An XML processing instruction." This is necessary the XML parsers
"Would recognize this document as an XML document."

Set OBJPI = objdom.createprocessinginstruction ("xml", "vertsion=" 1.0 "")

"Call the InsertBefore method of the ' xmldom Object in ' 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 would save this XML
"Document to your disk drive. In this case, the document would be saved
"To the" C: "Drive and would be named" Myxmldoc.xml ". When saving an
"XML document, if the file does not exist, it'll be created." If it
"Does exist, it'll be overwritten.

Objdom.save "C:myxmldoc.xml"

%>


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>
<table border= "1" >
<form action= "processform.asp" method= "POST" >
&LT;TR&GT;&LT;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>
&LT;TR&GT;&LT;TD colspan= "2" align= "center" ><input type= "Submit" id= "Btnsub" name= "Btnsub" value= "Submit" > </td></tr>
</form>
</table>
</body>


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

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 ' was 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 ' 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 ("Fieldvalue")
' Set ' The value of the Field_value element equal to
' The ' value of the ' current field ' 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
' Don't break on ' a error.
On Error Resume Next
' Call the Convertformtoxml function, passing on the physical path to
' Save the ' file to and the ' name ' of you wish to use for the file.
Convertformtoxml "F:\Asp\", "Contact.xml"
' Test to ' if ' a error occurred, if so, let the user know.
' Otherwise, tell the user this 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
%>


If you are using the above code in your own application, keep in mind that if the "convertformtoxml" function is already running, the file will be overwritten if the XML file name already exists. Here, I recommend that you use a randomly created file name before using the "Convertformtoxml" feature. In this way, the risk of overwriting valuable data is reduced to zero.

For an example of the generation of XML files, examples are as follows:


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>


I recommend that you copy the above code to a page on your personal Web server with the same name and run the example above. Make sure you are using a valid path and file name for your personal server.

When you are all ready, please check your XML file again.




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.