Java – Object XML Mapping
November 11, 2009 3 Comments
Have you written 1000s of buggie lines of code to parse XML? Generating an XML is easy but when it comes to XML parsing/import it is hard. We have to think about many scenarios and deal with many combinations. I use to prefer XPath API for XML import because I can fetch required values as they way/order I want rather than iterating all nodes in DOM structure. XPath is better than DOM coding but is not the best solution. So I started looking for a solution similar to Hibernate for XML to Object translation.
My first look was Commons Digester to translate XML to Java Object. I gone through a few page of their document they explain how to create mapping XML to map XML tags for Java classes but it is not like Hibernate where we can give RDBMS tables definition and it will generate equivalent Java classes. So I kept my search ON found a few open source tools which can generate Java classes from XML Schema but none worked as there was no activity for long time. Finally I found JAXB API which exactly meets my expectation.
Advantages of JAXB
- Java classes can be generated from DTD or Schema definition.
- Marshall (XML export) and Unmarshall (XML import) are part of JRE hence no additional jars required.
- Easy/Simple to use API.
- You can enjoy writing more Java code (i.e. business logic) and no parsing code.
Added Nov 17, 2009
While writing this post I remember the below quote:
“Complexity is a sign of technical immaturity. Simplicity of use is the real sign of a well design product whether it is an ATM or a Patriot missile.”
- Daniel T. Ling