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

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
spgif spgif
ASP ASP.NET ASP FAQs Feedback topnav-right
Print this page.
Published: Thursday, December 28, 2000

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!

- continued -

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:
 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
 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 "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.
 objDom.Save "c:\MyXMLDoc.xml"

Once the document has been saved, if you open the document it will look like the following code listing.

<?xml version="1.0"?>
 <childElement1 />
 <childElement2 />

In the MyXMLDoc.xml document, the childElement1 and childElement2 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!

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