views:

405

answers:

3

Out of all the libraries for inputing and outputting xml with java, in which circumstances is commons-digester the tool of choice?

+1  A: 

From the digester wiki

Why use Digester?

Digester is a layer on top of the SAX xml parser API to make it easier to process xml input. In particular, digester makes it easy to create and initialise a tree of objects based on an xml input file.

The most common use for Digester is to process xml-format configuration files, building a tree of objects based on that information.

Note that digester can create and initialise true objects, ie things that relate to the business goals of the application and have real behaviours. Many other tools have a different goal: to build a model of the data in the input XML document, like a W3C DOM does but a little more friendly.

and

And unlike tools that generate classes, you can write your application's classes first, then later decide to use Digester to build them from an xml input file. The result is that your classes are real classes with real behaviours, that happen to be initialised from an xml file, rather than simple "structs" that just hold data.

As an example of what it's NOT used for:

If, however, you are looking for a direct representation of the input xml document, as data rather than true objects, then digester is not for you; DOM, jDOM or other more direct binding tools will be more appropriate.

So, digester will map XML directly into java objects. In some cases that's more useful than having to read through the tree and pull out options.

Stephen Pape
A: 

If you want to create and intialize "true" objects from XML, use a decent bean container, like the one provided by Spring.

Also, reading in the XML and processing it yourself using XPath, or using Java/XML binding tools like Castor, are good and maybe more standard alternatives.

I have worked with the Digester when using Struts, but it seems that it has been surpassed by other tools and frameworks for the possible uses it has.

eljenso
+1  A: 

My first take would be "never"... but perhaps it has its place. I agree with eljenso that it has been surpassed by competition.

So for good efficient and simple object binding/mapping, JAXB is much better, or XStream. Much more convenient and even faster.

StaxMan