Interact with XML - the article concentrates in appending but the ZIP includes a full project.
Note: It saves the XML file onto a specific folder. Please change this in the code.
This example assumes that you are familiar with ASP, XML and HTML 4.0.
Before proceeding with this article, I recommend that you read Saving HTML Form Data to XML and Editing XML with XSL and ASP.
The example in this article is very similar to the example on Saving HTML Form Data to XML. However, instead of creating a new file each time that the form is submitted, we will check to see if the file already exists, and if it does, we will append to it. Using this type of technique, we are creating a data construct similar to a database table.
If you have read the Saving HTML Form Data to XML article, you will notice that the code to append the HTML data to the XML file is almost identical to the code used to save HTML data to a new file. There are two main differences in the addNewContacttoXML
function used in this example and the ConvertFormtoXML
function used in the Saving HTML Form Data to XML example.
This section of code is from the addNewContacttoXML
function. Since we are not creating a new file each time, we save a contact now, we need to call the Load
function of the XMLDOM
object and attempt to load the file. If we are able to load the file, we obtain a reference to the root element of the XML document. If the file does not load, we assume that it does not exist and create reference to a new root element and append it to the XML Document.
The other main difference is when we test a second time to see if the file was successfully loaded so that we know whether or not we need to add a processing instruction. If the file exists, we do not need a processing instruction. However, if a new file is being created, a processing instruction must be added.
Other than these two differences, however, you will notice that the code to save data to a new file is virtually the same as the code to append a new record to an existing file. We create a new container element, contact, as a child of the root element, main, to hold the data for each new record added. The code then iterates through the Form Collection of the Request Object creating the appropriate XML nodes and setting the nodes' values equal to the current Form Field.
As always, it is recommended that you copy this code to your development server and give it a try. I hope that this example will help shedding some light on the subject!