Click here to Skip to main content
15,886,578 members
Articles / Programming Languages / ASP
Article

XML function

Rate me:
Please Sign up or sign in to vote.
2.25/5 (4 votes)
26 Feb 20022 min read 113.3K   450   30   10
Read and write XML files.

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!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalhey Pin
Nish Nishant27-Feb-02 13:38
sitebuilderNish Nishant27-Feb-02 13:38 
GeneralRe: hey Pin
Mauricio Ritter28-Feb-02 1:09
Mauricio Ritter28-Feb-02 1:09 
GeneralRe: hey Pin
Chris Maunder28-Feb-02 4:36
cofounderChris Maunder28-Feb-02 4:36 
GeneralRe: hey Pin
Nish Nishant28-Feb-02 13:57
sitebuilderNish Nishant28-Feb-02 13:57 
GeneralRe: hey Pin
Chris Maunder1-Mar-02 3:33
cofounderChris Maunder1-Mar-02 3:33 
GeneralRe: hey Pin
Mike Klimentiev4-Mar-02 10:37
Mike Klimentiev4-Mar-02 10:37 
GeneralRe: hey Pin
Braulio Dez4-Mar-02 21:02
Braulio Dez4-Mar-02 21:02 
GeneralMissing Zip! Pin
Nish Nishant27-Feb-02 2:30
sitebuilderNish Nishant27-Feb-02 2:30 
GeneralRe: Missing Zip! Pin
Carlos Antollini27-Feb-02 8:01
Carlos Antollini27-Feb-02 8:01 
GeneralRe: Missing Zip! Pin
Chris Losinger27-Feb-02 8:11
professionalChris Losinger27-Feb-02 8:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.