JAXB is the standard and the best approach for coverting Java objects to XML. There are several open source implementations available:
For more information on JAXB check out my blog:
UPDATE:
What is the BEST approach?
This ultimately depends on what you are trying to do, I'll explain below:
Use Case #1 - Starting from an XML Schema
In this use case you have an XML schema and you want to generate a Java model. Not many of the tools mentioned in this thread support this use case. XStream for example recommends XMLBeans for this.
Nominees:
- JAXB (all implementations) - Generates POJOs with JAXB annotations.
- XMLBeans - Generates proprietary classes that include XML binding logic.
Use Case #2 - Starting from Java Classes (that you can edit)
In this use case you have much more selection (only XMLBeans is eliminated). The edits normally involve the addition of annotations to control the mapping.
Nominees:
Use Case #3 - Starting form Java Classes (that you can not edit)
In this use case you do not have the source to modify the model classes. This requires the metadata to be supplied externally either with an XML file of by code.
Nominees:
- EclipseLink JAXB (MOXy) - Offers an external binding file, and metadata can be applied programmatically.
- Metro JAXB - Can leverage Annox or JAXBIntroductions
- Castor - Offers an external binding file
- JiBX - Offers an external binding file
- XStream - Metadata can be applied programmatically
Use Case #4 - Meet-in-the-Middle (Existing classes and schema)
In this use case you have existing classes that you need to map to an existing XML schema. EclipseLink MOXy with its XPath based mapping is the only tool I'm aware of that can handle this use case
Nominees:
Use Case #5 - XML Infoset Preservation:
In this use case you need to preserve the unmapped content: comments, processing instructions etc.
Nominees:
- JAXB (all implementations) - Has the Binder feature.
- XMLBeans - The generated object model stores the entire XML infoset.
Use Case #6 - Compatibility with JPA
JPA is the Java standard for Java persistence. JPA has many concepts: composite keys, bidirectional relationships, lazy loading, etc that can be hard to use with an XML binding solution. For example any XML tool that only interacts with objects via the field will usually have problems with lazy loading properties.
Nominees:
Use Case #7 - Compatibility with XML Web Services (JAX-WS)
JAXB is the default binding layer for JAX-WS.
Nominees:
- JAXB (implementation depends of the JAX-WS provider)
Use Case #8 - Compatibility with RESTful Web Services (JAX-RS)
JAX-RS offers a light-weight alternative to JAX-WS based on the HTTP protocol. Check out the following for an example.
Nominees:
- JAXB (all implementations) - The default binding layer and easiest to use with JAX-RS.
- Everything else - You can leverage the concepts of MessageBodyReader/Writer to use other XML tools.
Use Case #9 - Compatibility with Spring
Spring has some built in support for integrating with XML binding tools, check out the following link for more information:
Nominees:
- JAXB (all implementations)
- Castor
- XMLBeans
- JiBX
Other Things to Consider
- Is the tool still being developed/supported? As funny as this sounds I've seen people recommend tools that haven't beed updated in 5 years. Some of the tools mentioned here haven't released in 2 years.
My Pick for BEST approach? - JAXB
Looking at the above categories, JAXB may not always be the best fit for a particular use case (but it is always a good fit), but it is the only library that can be used for all the use cases. This means it can always do the job. The alternative is to use different libraries for different tasks giving you multiple libraries to support.
I do lead a JAXB implementation EclipseLink MOXy, but MOXy began its life as a proprietary XML binding library TopLink OXM. TopLink has always understood the benefit of standards (i.e. EJB/JPA), and we implemented JAXB 1. Then we (I am the represetative) became active members on JAXB 2 (JSR-222).
XStreamstack up withJAXB?