When and why would you use Apache commons-digester? - java

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

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.

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.
EDIT 2019: also, Jackson XML, similar to JAXB in approach but using Jackson annotations

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.

Related

Best Practice for large XML file builder

I have to build an XML file for an input to a SOAP service in Java. The input xml can consist of at least 1000 tags. What is the best way to build the XML? I have the XSD files but it is a bit complicated to use JAXB. Is XMLStreamWriter a good option for that?
XMLStreamWriter is one of the better APIs to use for writing XML from a Java application, but it has a few quirks (e.g. its namespace handling is a bit bizarre) and you may find it worthwhile to wrap it in a convenience API that knows about the kind of document you are writing, e.g. what namespaces it uses.
One of the advantages of the XMLStreamWriter interface is that there are plenty of implementations to choose from. For example Saxon has an implementation that gives you full control over all the XSLT/XQuery serialization options plus Saxon extensions (for example, you can even control the output order of attributes!)
One of the problems I hit with all event-based APIs is that sooner or later you find yourself forgetting to write an end tag, and that can be quite tricky to debug. Using a wrapper API that forces you to include the element name in a call on endElement() can be useful for debugging; if debugging is switched on you can keep a stack of element names and check that endElement() is writing the right tag; with debugging switched off you just drop this check.
Serializing using JAXB is higher-level, of course, but the downside is that it gives you less control.

Java - Apache commons Digester - write xml

I'm using Apache Commons Digester (with annotations) in order to load an XML file into a Java class.
Everything works correctly.
Now, I need to update the XML file. I have to change (in Java) the value of a property, and then to write out the new XML file.
How could I do? As far as I can see, Digester API is not designed for this purpose.
Edit: reading the answers, I understand that I did not give enough informations. My XML file is a configuration file for a program A, so I really need its content when I launch program A. Then, I have another GUI program B that is able to modify this configuration file, it just takes some input from the user and modifies the relative fields on the XML file.
As you have found, Digester is a read-only tool - it provides a mapping from XML to Java classes, but not the reverse. If you need to read the XML into Java classes and then write it back to XML again, I would suggest either:
Keep using Digester for the reading, and use a low-level XML writer
class (such as XMLStreamWriter)
to write the data back. This is suitable if your data is not that
complex, and/or the output XML is a different structure to the input
XML.
Replace Digester with a full Java to XML mapping library (JAXB, JiBX
etc.), which will both read and write the XML for you. This is
suitable if your data is more complex, and/or the output XML is the
same structure as the input XML.
Don't know enough about what you are doing to really recommend one of those approaches over the other.
If you don't actually need the data in Java classes at all and are just trying to transform it, then XSLT as #sharonbn says is also a good solution.
XML modification (usually called XML transformation) is best handled in XSLT standard. Apache Xalan is (one of) the Java libraries that implement this standard

Java library to assist in XSD creation?

Is anyone aware of a library that makes the process of creating XSDs in Java a little easier than using something like a DocumentBuilder? Something like a helper or utility class. I came across the org.eclipse.xsd maven jar but I'm having ClassNotFoundException issues when working with it in Eclipse and I'm not entirely sure it's meant to be used as a standalone kind of thing. This is a bit difficult to Google for as well since there are lot of search results around automatic generation/translation from Java to XSD and vice versa.
Essentially what I need to do is to programmatically create an XSD from a certain source of data -- not Java classes.
Apache XMLSchema is a lightweight Java object model that can be used to manipulate and generate XML schema representations. You can use it to read XML Schema (xsd) files into memory and analyze or modify them, or to create entirely new schemas from scratch.
The fact that with this API one can create an XSD from scratch, it sounds as a starting point to achieve the ask; as to the fitness, it depends on what that "certain source of data" is.

File Manipulation libraries

I have a project which I need to manipulate files. things like: create new file by a defined structure(header,data,trail). and then I need to things like search/validate/create/read.
basically I want to map the files to objects and vise versa.(I am willing to map them to objects coz it will be much more comfortable for me to manipulate the fields inside each file via object)
I wonder if any of you deal with such things before? and maybe could recommend me on libraries which could easy my work.
thanks,
ray.
You may want to look at serialization and de-serialization
If you want custom mapping, you need custom coding. I would suggest you look at DataInputStream and DataOutputStream.
Using these you can control the header, records and footer in any binary format you want.
I suggest you generate your serialization (if you need to have the afstest possible speed) or use reflections to do the translation. Just using reflection is pretty fast and much simpler than generating code. ;)
In the end I Found a ORM framework called Canyon which mapping Files to Objects. but still had difficulties. so I have implemented my own ORM file to objects and vise versa.
If you have a defined file layout with different content you should consider to use a template engine like FreeMarker or Velocity to generate your files.
You can define templates here which will be filled with your dynamic content which you have to provide. Definitly better than to use System.out (I mean hard code your template text).
A library which helps for basic file manipulation is Apache Commons IO.
If you realy want to map your files to objects than it would be a Serialization/Deserialization as Angelom mentions. Many libraries help you to do this but the file format is fixed:
JSON: Jackson, GSON
XML: JAXB
If you want the file read by 3rd party as well, how about using some popular existing exchange format such as CSV or XML?
XML is fully supported in standard library. There's plenty of CSV libraries out there, including Apache Commons CSV.

Java Collada Parser - XML Pull based implementation

I am looking at a set of parsers generated for Atom, XAL, Kml etc. seemingly using an automated technique with a XML pull based parser. The clue towards the automation is presence of "package.html" in all XML-to-Java mapped classes folders. I would like to produce a similar one for the rather large Collada 1.4 spec. My first attempt with Altova ran into small problems due the "enum" keyword. I am sure I can fix it in the next run with appropriate renaming. Khronos admit to not designing the 1.4 spec to being automated parser generation friendly.
The actual parsers i.e. XAL parser, Atom parser etc. implement the XMLEventParser interface. I would like to know if anybody has encountered/used this pattern. If so which tool can be used to map the XSD to a class set simply giving access to the data components of the nodes using getters and setters.
I'm not sure I understand your question, but it appears that you want to process XML formats like Atom and represent it in objects with getters/setters. This can easily be done with JAXB.
For an example see:
http://bdoughan.blogspot.com/2010/09/processing-atom-feeds-with-jaxb.html

Categories