Saving HTML Form Data to XML by ASP

Storing your form submissions in XML

Usually form submissions in ASP are written to some sort of database management system. However, if you need your form submission data to be more portable, it can be written to an XML file. This is especially useful when the data you are gathering on your Web site will be sent to applications on non-Windows platforms. Since XML is completely portable across all platforms the data will not need to be converted.

In order to write a form submission to an XML document, it is necessary to create a new XML document using the Microsoft XMLDOM Object. The XMLDOM Object has an extensive object library that can be used to create the elements, attributes, and values that will make up the XML document. I will not cover the entire object model, because it is very extensive and could make up an entire section of this Web site by itself.

After the XMLDOM Object has been instantiated, the structure of the XML must be laid out by creating object references to the elements that make up each layer of the XML document. The following is an example of how the XMLDOM would be instantiated and a reference to the root element created. After the root element is created, it is appended to the XMLDOM Document. Then child elements are created and appended to the root element and then the document is saved.

Instantiating the Microsoft XMLDOM Object:

 Dim objDom
 Dim objRoot
 Dim objChild1
 Dim objChild2
 Dim objPI
 'Instantiate the XMLDOM object using the CreateObject Method of the
 'Server Object.
 Set objDom = Server.CreateObject("Microsoft.XMLDOM")
 'Create a reference to an IXMLDOMElement (XML Element) Object by
 'calling the createElement Method of the XMLDOM. The createElement
 'Method accepts one paramter, a string representing the name of the
 'element. The return value is passed to the objRoot variable. This
 'element reference will represent the root element of the XML
 Set objRoot = objDom.createElement("rootElement")
 'Use the appendChild Method of the XMLDOM Object to add the objRoot
 'Element Reference to the XML Document.
 objDom.appendChild objRoot
 'Now, following the same steps, you will create references to the
 'child elements for the XML Document. The only difference is, when the
 'child elements are appended to the document, you will 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, you are differentiating (and applying tiered
 'structure to) the child elements from the root element.
Set objChild1 = objDom.createElement("childElement1")
 objRoot.appendChild objChild1
 Set objChild2 = objDom.createElement("childElement2")
 objRoot.appendChild objChild2
 'The final step to take care of before saving this document is to add
 'an XML processing instruction. This is necessary so that XML parsers
 'will recognize this document as an XML document.
 Set objPI = objDom.createProcessingInstruction("xml","version='1.0'")
 'Call the insertBefore Method of the XMLDOM Object in order 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 will save this XML
 'document to your disk drive. In this case, the document will be saved
 'to the "c:" drive and will be named "MyXMLDoc.xml". When saving an
 'XML document, if the file does not exist, it will be created. If it
 'does exist, it will be overwritten.
 objDom.Save "c:\MyXMLDoc.xml"

Once the document has been saved, if you open the document it will look like the following code listing.


<?xml version="1.0"?>
 <childElement1 />
 <childElement2 />

In the “MyXMLDoc.xml” document, the childElement1 and childElement2 elements are rendered as empty elements. If they contained values each would have been rendered with opening and closing tags enclosing the value of the element.

Now, lets consider the idea of writing an HTML form submission to an XML document. We saw above how we could create and save an XML document. Writing a form submission to an XML document just becomes a matter of iterating through the Request Object’s Form Collection and writing the value of each form field to an XML element value. This can be accomplished with Active Server Pages.

Example: Sending Form Submissions to XML

Below is a normal HTML form that we will be using for this example. This form asks for the user’s name, address, telephone number, and e-mail address. This information will be written to an XML file for storage.


  Contact Information
 <form action="processForm.asp" method="post">
  <h3>Enter your contact information</h3>
  First 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>

The action for this HTML form is set to processForm.asp. This is an ASP page that will call a function which iterates through the form fields and writes their values to an XML file.


'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
   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 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
 '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. 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 "c:","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.")
 Response.write("Your form submission has been saved.")
End If

If you use this code in your own applications, remember one thing, when the “ConvertFormtoXML” Function runs, if the XML file name specified already exists, it will be overwritten. I suggest dynamically creating random file names before calling the “ConvertFormtoXML” Function. That way you will never risk overwriting any valuable data.

The XML file that would be produced by this example would appear (something similar to) as follows:


<?xml version="1.0" ?>
 <field id="firstName" taborder="1">
 <field id="lastName" taborder="2">
 <field id="address1" taborder="3">
  <field_value>2129 NW 27th St.</field_value> 
 <field id="address2" taborder="4">
  <field_value /> 
 <field id="phone" taborder="5">
 <field id="email" taborder="6">

I would suggest copying the code for EnterContact.htm and processForm.asp to pages of the same name on your Web server and running the example. Make sure that you use a path and file name that work for your server. Examine the XML file when you are done.

If you’d like to play with the form, here’s a working demo for you.

The original post can be found here.



One thought on “Saving HTML Form Data to XML by ASP

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s