Saving HTML Form Data to XML
By Michael Qualls
|For More Information on XML|
To learn more about XML, be sure to check out the XML Article
Index, which contains numerous links to various informative XML-related articles scattered
across the Web!
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 usefull 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. At the end of this article I will
recommend several excellent resources on the XMLDOM.
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. Then the document is saved.
Instantiating the Microsoft XMLDOM Object:
'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 "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.
Once the document has been saved, if you open the document it will look like the following
MyXMLDoc.xml document, the
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.
In Part 2 we'll look at how to use the XMLDOM Object
to save HTML form-based user inputs as XML!
Read Part 2!