You can use a XML file as database to store different type of information.
On this article I will show you how to use XML file as a storage area later.
When you store information on xml file you will save time and system resource if you use access database the process is more complicated, an Access database have limits on the number of users using it at a given time while XML file does not, even more the XML files is a standard way of publishing data on the web. And if you use XML you can share you resource and you can even publish your content over.
So let's get started.
To connect to and XML file you create a connection object as with any kind of databases, but for xml files it's not and ADO connection
The below line of code would create the connection object
Set ObjXML = Server.CreateObject("Microsoft.XMLDOM")
then you load the file in this case it's
blnFileExist = objXML.load(server.mappath("myfile.xml"))
if the file exist we will continue and make a new record if it's not then we will start over
If blnFileExist = False Then
ObjXML.appendChild(objXML.createProcessingInstruction("xml", "version=""1.0"""))
ObjXML.appendChild(objXML.createElement("user"))
IntID = 1
Else
IntID = objXML.documentElement.childNodes(objXML.documentElement.childNodes.length - 1).childNodes(0).text + 1
End If
then the operation would change depending on what you want to do with the file.
In the following example I will insert some data into the XML file
First I will create the elements that I will later insert the data into
Set objXMLe = objXML.createElement("user")
ObjXMLe.appendChild(objXML.createElement("ID"))
ObjXMLe.appendChild(objXML.createElement("UserName"))
ObjXMLe.appendChild(objXML.createElement("Email"))
then you insert the data into the elements and you are almost done.
ObjXMLe.childNodes(0).text = intID
ObjXMLe.childNodes(1).text = Request.form("username")
ObjXMLe.childNodes(2).text = Request.form("email")
You have to save this file now
ObjXML.documentElement.appendChild(objXMLe.cloneNode(True))
ObjXML.save(server.mappath("myfile.xml"))
At last you clear the connection to save system resources
Set objXMLe = Nothing
Set objXML = Nothing
You can download all the code on a zipped file OR see a Live Demo