XML Persistence

I really don’t like having to mess with databases if I don’t have to, and it doesn’t really make sense to for a small site anyway. So when I implemented this little blog the other night, I decided to use text files to store my data. Now, I could easily use a comma-separated format to do so, and that would no doubt work fine for a simple blog like this, but when you start to introduce some complexity (like associating comments with posts), the work of parsing text files doesn’t sound so appealing.

That’s when XML starts to seem like the right solution. Problem is, it’s much more of a pain in the neck to parse than comma-separated text. Of course there are XML parsers out there, but even so, you have to navigate the XML tree and nodes and all that. What I really wanted to do is save my Java objects without worrying about the file format.

So I searched around the web for a bit, and came across a lovely package called Castor. With basically one line of code, you can save a Java object to a file. And not just simple objects with primitive data members - feel free to nest your data structures! You don’t have to parse XML, navigate a tree, anything. You just work with your own objects, and Castor figures out how to turn them into XML for you.

Even better, Castor is free and open-source!

Note: this blog now uses Wordpress, and therefore is using a MySQL database, but I still plan on using Castor for other projects!

Comments are closed.