Thursday, January 26, 2006

JDOM 1.0 Released!

The title is a little misleading. Yes, JDOM 1.0 has been release, but it was released about a year and a half ago. For me it is a new discovery though, and one worth mentioning for those who haven't used it.

Why is JDOM 1.0 worth the mention? Well, they did good. They took the DOM specification, and built an API around it that doesn't hurt my head when I use it. It is almost exactly what I would want in a DOM API.

There is a story behind this, and it goes way back to the summer of 2000. I was writing a lot of Perl code at the time, and I was working with the DOM a little bit. One Friday night before I left work I got to thinking about something I had read on that day where the author described something as "Perlish". I decided that the Perl DOM implementation was not Perlish. I went home that night, and got to work.

What I came up with was something very Perlish. I would access elements through the API, and if they didn't exist, then they would be magically created. I could loop through elements by name, and set text and attributes with a simple method call. No longer did I need to iterate through the child nodes of an element searching for text and CDATA nodes. My API wasn't a DOM replacement though, it was just a wrapper around the DOM API, and made it trivial to use.

The Perl module I wrote is called XML::EasyOBJ, and it came with a quick start guide that claims that someone who doesn't know the XML DOM can be using my wrapper API in 10 minutes (I still claim that). To this day I still use this module for both personal and professional use.

I have strayed a bit from JDOM, but my Perl module is related. Like I said, JDOM is almost what I would want in a DOM API, except that it doesn't allow for auto-creation on elements. I have started building an API for this around JDOM, which may fill most of my needs. Here is a sample of what the code looks like:

EasyDocument doc = new EasyDocument("rootTag");

This prints:
<rootTag><foo><bar /></foo></rootTag>

In a similar fasion you can specify the index of the element. If you specify an index that is out of bounds, it will automatically create the elements.

EasyDocument doc = new EasyDocument("rootTag");
doc.e("foo").e("bar", 2).setText("test);

This prints:
<rootTag><foo><bar /><bar /><bar>test</bar></foo></rootTag>

So... what is my point? That is a good question. Perhaps I am expressing my distaste of complex API's. Perhaps I am just so pleased with JDOM that I wanted to say it out loud. Perhaps I wanted to show my genius in creating an even simpler DOM API. Or maybe the point I am trying to make lies somewhere in between.


1 comment:

Anonymous said...

Hi, Your old email in the XML::EasyObj doesn't work.

I can't seem to get $doc->makeNewNode('name'); to work. I'm printing the results out at the end by geting the underlying dom object then calling its printToFile() method.

How do I handle elements with a hyphen in their name?

my email is, funkular at hotmail dot com.