Saturday, January 17, 2009

JDOM For Generating XML

As you've probably figured out from my blog, I'm an ASP.NET C# web developer who loves trying out alternative technologies. There has always got to be another way to do something and if there isn't, then it's time to fire up the IDE and write some open source software!

After being pretty impressed with the XML API for writing XML documents with the Microsoft .NET Framework, I wanted to check out what Java had to offer. At the start, it was pretty confusing as there were so many different libraries and APIs so I didn't know what to pick. When I asked around, a developer suggested that I try out JDOM. JDOM is a pretty simple library for working with XML and is pretty close to Microsoft .NET's API. I decided to take it for a spin and here's what I came up with:

import org.jdom.*;
Document doc = new Document();
Element elroot = new Element("root");
elroot.setText("This is the root");
output.XMLOutputter xo = new output.XMLOutputter();
FileOutputStream fos = new FileOutputStream("xmlout.xml");
xo.output(doc, fos);

JDOM's simplicity makes it very appealing to use when dealing with small amounts of data.

1 comment:

dontcare said...

Have you looked at vtd-xml? it is a lot faster and memory efficient than DOM4J and JDOM