xml| Tutorials | Getting Started | data
Typically, we store data in a database. However, if you want the data to be more portable, we can store the data in an XML file.
Create and save an XML file
If the data is to be transferred to an application on a non-Windows platform, it is advantageous to save the data in an XML file. Keep in mind that XML has a strong cross-platform portability and that data does not need to be converted!
First, we'll learn how to create and save an XML file. The following XML file will be named "Test.xml" and stored in the C directory on the server. We will use ASP and Microsoft's Xmldom object to create and save this XML file:
<%dim xmldoc, Rootel, Child1, Child2, p ' Create the XML Document Set xmldoc = Server.CreateObject ("microsoft.xmldom") ' Create the root element and add it to the document Set Rootel = xmldoc.createelement ("root") xmldoc.appendchild Rootel ' Create and add child element Set child1 = Xmldoc.createelement ("Child1") Set child2 = xmldoc.createelement ("child2") rootel.appendchild child1rootel.appendchild child2 ' Create XML processing Instruction ' and add it to the root element before set p=xmldoc.createprocessinginstruction ("xml", "version= ' 1.0 '") Xmldoc.insertbefore p, Xmldoc.childnodes (0) ' Save the file to the C directory xmldoc.save ' C:\test.xml '%>
If you open this saved file, it will make this look ("Test.xml"):
<?xml version= "1.0"?><root> <child1/> <child2/></root>
Real examples of forms
Now, let's look at a real example of a form.
Let's take a look at this HTML form used in the example: The following HTML form requires users to enter their name, nationality, and e-mail address. The information is then written to an XML file for storage.
"Customers.htm":
The action for the above HTML form is set to "saveform.asp". The "saveform.asp" file is an ASP page that iterates through form fields and stores their values in an XML file:
<%dim Xmldocdim Rootel,fieldname,fieldvalue,attiddim p,i ' If an error occurs, the program is not allowed to terminate on error Resume Nextset = server. CreateObject ("Microsoft.XMLDOM") Xmldoc.preservewhitespace=true ' creates and adds the root element set Rootel to the document = Xmldoc.createelement (" Customer ") Xmldoc.appendchild Rootel ' loops through form set for i = 1 to Request.Form.Count ' removes the button element in the form if InStr (1,request.form.ke Y (i), "Btn_") =0 then ' Create field and value elements, and ID property Set fieldName = Xmldoc.createelement ("field") Set fieldvalue = X Mldoc.createelement ("value") Set Attid = Xmldoc.createattribute ("id") ' Sets the name of the current form field to the value of the id property Attid.text = request.fo Rm. The Key (i) ' Adds the id attribute to the field element Fieldname.setattributenode Attid ' Sets the value of the current form field to the value Fieldvalue.text = Request.Form ( i) ' Add a field element as a child of the root element rootel.appendchild fieldName ' Add the value element as a child of the field element Fieldname.appendchild FIELDV Alue End IfNext ' Add XML processing instruction ' and add it to the root element before set P = xmldoc.createprocessinginstruction ("xml", "version=" 1.0 ' ") Xmldoc.insertbefore p,xmldoc.childnodes (0) 'Save XML file Xmldoc.save "C:\Customer.xml" to release all object references set Xmldoc=nothingset Rootel=nothingset Fieldname=nothingset Fieldvalue=nothingset attid=nothingset p=nothing ' test if there is 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 specified XML file name already exists, the file will be overwritten!
The XML file is generated from the above code, roughly like this: ("Customer.xml"):
<?xml version= "1.0"?><customer> <field id= "FirstName" > <value>Hege</value> </field> <field id= "LastName" > <value>Refsnes</value> </field> <field id= "Country" > <value>Norway</value> </field> <field id= "email "> <value>mymail@myaddress.com</value> </field></customer>