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:
<%
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
'Document.
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 "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.
objDom.Save "c:\Mydoc.xml"
%>
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:
<%
dim xmlDoc
dim rootEl,fieldName,fieldValue,attID
dim p,i
'Do not stop if an error occurs
On Error Resume Next
Set xmlDoc = server.CreateObject("Microsoft.XMLDOM")
xmlDoc.preserveWhiteSpace=true
'Create a root element and append it to the document
Set rootEl = xmlDoc.createElement("customer")
xmlDoc.appendChild rootEl
'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
fieldName.setAttributeNode attID
'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
rootEl.appendChild fieldName
'Append the value element as a child of the field element
fieldName.appendChild fieldValue
end if
next
'Add an XML processing instruction
'and insert it before the root element
Set p = xmlDoc.createProcessingInstruction("xml","version='1.0'")
xmlDoc.insertBefore p,xmlDoc.childNodes(0)
'Save the XML file
xmlDoc.save "c:\Customer.xml"
'Release all object references
set xmlDoc=nothing
set rootEl=nothing
set fieldName=nothing
set fieldValue=nothing
set attID=nothing
set p=nothing
'Test to see if an error occurred
if err.number<>0 then
response.write("Error: No information saved.")
else
response.write("Your information has been saved.")
end if
%>
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"):
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords: xml document, document object model, text node, child node,save xml data, xml sample, xml to data, xml schema,xml parsing,xml editord, xml encoding, xml namespace.