Form submissions in ASP are written to some sort of database management system.
A D V E R T I S E M E N T
However, if you want 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 data not need to converted , it is completely portable across all platforms.
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 attributes,elements, and values that will make up the XML document. Here we are 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.
Creating and Saving an XML File
If the data is to be sent to applications on non-Windows platforms Storing data in XML files is useful.
Remember that data will not need to be
converted because XML is
portable across all platforms
Now we learn how to create and save an XML file. The
XML file below will be named "Mydoc.xml" and will be stored in
the c directory on the server. We will use both ASP and Microsoft's
XMLDOM object to create and save the XML file:
'Instantiate the XMLDOM object using the CreateObject Method of the
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.
'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")
Set objChild2 = objDom.createElement("childElement2")
'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 "Mydoc.xml". When saving an
'XML document, if the file does not exist, it will be created. If it
'does exist, it will be overwritten.
After save your document if you open the document it will look like the following code listing.
The action for the HTML form above is set to "saveForm.asp". The "saveForm.asp" file is an ASP page that will loop through the form fields and store their values in an XML file:
'Do not stop if an error occurs
On Error Resume Next
Set xmlDoc = server.CreateObject("Microsoft.XMLDOM")
'Create a root element and append it to the document
Set rootEl = xmlDoc.createElement("customer")
'Loop through the form collection
for i = 1 To Request.Form.Count
'Eliminate button elements in the form
if instr(1,Request.Form.Key(i),"btn_")=0 then
'Create a field and a value element, and an id attribute
Set fieldName = xmlDoc.createElement("field")
Set fieldValue = xmlDoc.createElement("value")
Set attID = xmlDoc.createAttribute("id")
'Set the value of the id attribute equal to the name of
'the current form field
attID.Text = Request.Form.Key(i)
'Append the id attribute to the field element
'Set the value of the value element equal to
'the value of the current form field
fieldValue.Text = Request.Form(i)
'Append the field element as a child of the root element
'Append the value element as a child of the field element
'Add an XML processing instruction
'and insert it before the root element
Set p = xmlDoc.createProcessingInstruction("xml","version='1.0'")
'Save the XML file
'Release all object references
'Test to see if an error occurred
if err.number<>0 then
response.write("Error: No information saved.")
response.write("Your information has been saved.")
Note: If the XML file name specified already exists,
it will be overwritten!
The XML file that will be produced by the code above will
look something like this ("Customer.xml"):