When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

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.

EnterContact.html:
<html>
<head>
 <title>
  Contact Information
 </title>
</head>
<body>
 <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>
 </form>
</body>
</html>

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.

processForm.asp:
<%
'--------------------------------------------------------------------
'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
   '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 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
 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


'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.")
Else
 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:

Contact.xml:
<?xml version="1.0" ?>
<contact>
 <field id="firstName" taborder="1">
  <field_value>Michael</field_value> 
 </field>
 <field id="lastName" taborder="2">
  <field_value>Qualls</field_value> 
 </field>
 <field id="address1" taborder="3">
  <field_value>2129 NW 27th St.</field_value> 
 </field>
 <field id="address2" taborder="4">
  <field_value /> 
 </field>
 <field id="phone" taborder="5">
  <field_value>4055253988</field_value> 
 </field>
 <field id="email" taborder="6">
  <field_value>michaelq@vertiscope.com</field_value> 
 </field>
</contact>

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


    Attachments:

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


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES