Continually Improving.... let us know how support@devdiamond.net Sign in | Sign up
home articles news blog forum  

 
 


 
Skip Navigation LinksHome > Article > How to use XML file as Database
How to use XML file as Database
Abstract :
Here is how you can use XML file as database... its easy, and you can learn about it, on this article you can even download the real examples, Good Luck

Views : 21394
Published : Wednesday, March 27, 2002
By
HyperLink

Avarage Rating :
Page Page 1 of 1

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



About Author

        Yasir Send Feedback
        Yasir is a .NET expert, with over 5 years experience in Microsoft Technologies, 8 years overall programming experience, he is the owner, founder & primary contributor of Minwar.com, and he also works as IT Director in the hospitality industry.