When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Thursday, December 28, 2000

Saving HTML Form Data to XML, Part 2

By Michael Qualls

Read Part 1

In Part 1 we looked at a simple code example that would create an XML document using the Microsoft XMLDOM Object. In this part, we'll look at how to use this object to store a user's input as an XML document!

- continued -

Let's 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.

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
  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.
 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

'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
[View a live demo!]

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.

The following books are excellent resources on using the Microsoft XMLDOM in your Web applications:

Happy Programming!

  • By Michael Qualls


  • View a live demo!
  • Visit the XML Article Index

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article