Friday, January 30, 2009

Goodbye to the old...

When I first started using Geronimo (the Apache JEE server), it was on version 'point something'. And, I needed to parse XML messages being sent from a browser to servlets.

So, I used JDOM. It was fairly simple to use and the JDOM libraries were included with Geronimo.

That made things very simple. All I needed to do to parse the XML messages was put something like this in the doPost method:

Reader reader = request.getReader();

try {
SAXBuilder builder = new SAXBuilder(false);
Document doc =;
Element root = doc.getRootElement();

Then, to get values out of the document, I wrote a number of wrapper functions like getString, getLong, getX - to convert the values in the XML document into something useful (something other than strings).

But, when Geronimo went from version 1.x to 2.x, they dropped the JDOM library from the assembly - And I started to have to include it myself.

Recently, I finally got tired of having to add the library myself and started to look for alternatives to using JDOM.

Enter W3C...

So, now I get to change a bunch of servlets from the code above to this new version:

Document document = getXML(request, dbf);

Ok, so that wasn't quite all that I did. That 'getXML' function isn't included with the W3C's DOM classes. I had to write it myself. And, here it is:

protected Document getXML(HttpServletRequest request, DocumentBuilderFactory dbf) {
Document document = null;

try {
DocumentBuilder db = dbf.newDocumentBuilder();

BufferedReader in = request.getReader();

String input = "";
String xmlText = "";

while((input = in.readLine()) != null) {
xmlText = xmlText + input;

System.out.println("Received: " + xmlText);

InputSource source = new InputSource();
source.setCharacterStream(new StringReader(xmlText));

document = db.parse(source);
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());


return document;

Now, that is much bigger (as far as lines of code) than the old way. But, by putting it into a function - you can't tell. And, once I am done with all of the change over, I will be able to get rid of that extra JDOM dependency.

Plus, along with finding how to change over to use the W3C's DOM, I also found the XPath libraries. So, I was able to change all of my XML to use real, properly formed documents. What I had been doing before was storing all of the real information in attributes of a single element.


1 comment:

dontcare said...

have you looked at vtd-xml, which is the latest and more advanced/powerful XML Processing API