Roll Your Own Java XML Database
By Stephen Bucaro
In the article Create a Database Using Only Notepad, I showed you how to create
a little database for yourself using only a simple text editor like Windows Notepad. You
could access that database using Internet Explorer. For security reasons, web browsers
are designed to prevent writing to the local hard disk. But with a workaround you could
save that database after you made updates.
In this article I show you how to write a simple Java program to access an XML
document that allows you to save your updates. The database for this example will be the
same one used in the previous article. It is an XML document containing all the different
modes which can be set for a computers display. It is just a simple flat database.
For your convenience I have zipped together the java source file and XML document
for the third example in this article, which you can download by clicking
here
XML, simply put, is tags added to a web page that describe the data on the page.
Because XML is a W3C standard, it also found a use in communicating between dissimilar
databases. I like XML because it is text, not binary data.
For efficiency most databases are stored in binary format and are not human readable
without the dbm (database management system). Several times I have lost databases because
they became corrupted. I wished I could open the database in a text editor to see what
was wrong and possibly make a repair.
Text databases are not a new idea. "comma delimited" text databases are commonly used
as small database applications. In fact a comma delimited text database might be superior
to an XML database because it doesn't have the overhead of XML tags. The advantage of an
XML database is that it's a standard. That means that your custom program is not the only
one that read it.
In the article Reading Files With Java, I showed you how to create a Java
program that could read a text file and display its contents in a scrollable window. The
first example in this article is a simple modification of the code from that article.
Depending upon your experience with Java programming, you might want to review that
article. If you have no experience with Java programming, you should read Getting
Started as a Java Programmer.
The second example in this article will provide your typical "Previous,Next,Find"
database interface. You could use this code when you don't want the user to be able to
modify the database. The third example will implement "Add, Delete", and "Save" functions.
Java is a good programming language because it is cross platform. That means your
program can run on Windows, Linux, and Macintosh without modification. You can easily
change a Java application into an applet that can be embedded in a web
page. Also, the Java Software Development Kit is a free download from Sun
Microsystems web site.
|