Java to XML conversions? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this questionWhat are different approaches to convert Java Objects
to XML
, I know one option is JAXB
but would like to know what are the other approaches/tools
available for the same ?
Note: I do not have further requirements and so I cannot add more meat
to the question but at this point of time it would be really great if I can get an idea of what different approaches are avai开发者_JS百科lable for converting Java to XML
?
Update: Different suggested approaches are:
- javax.xml.bind.Marshaller and javax.xml.bind.Unmarshaller
- XStream
- XMLBean
- JAXB
- Castor
- JIBX
- Apache Digester
Now among all the suggested approaches what is the BEST approach to go convert Java Objects to XML
and XML to Java Objects
?
JAXB is the standard and the best approach for coverting Java objects to XML. There are several open source implementations available:
- EclipseLink MOXy (I'm the tech lead)
- Metro (The reference implementation, included in Java SE 6)
- JaxMe
For more information on JAXB check out my blog:
- http://bdoughan.blogspot.com
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:
- Everyone but XMLBeans
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:
- EclipseLink JAXB (MOXy)
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:
- EclipseLink JAXB (MOXy) - Was built with JPA in mind.
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:
- http://static.springsource.org/spring-ws/docs/0.9.1/reference/oxm.html
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).
You can always do it yourself with javax.xml.bind.Marshaller
and javax.xml.bind.Unmarshaller
interfaces.
This isn't as crazy as it sounds. It's not that hard to do, and you'll have complete control over and knowledge of what's done.
I'm willing to acknowledge that lots of folks prefer an automated approach. My offering won't be popular.
I've used XStream in the past and liked it, but when I've used it I didn't have to worry about XML namespaces. I'm told that XStream doesn't deal with them well.
Some alternatives include:-
- XStream
- XMLBean
However, one significant advantage of going with JAXB is it ships with the Java distrubtion.
I know that this isn't what you asked, but have you considered JSON instead of XML? I'm currently working on a project that makes heavy use of JSON (with the Jackson library) and I'm very happy with it. Jackson works similarly to JAXB - it even works with JAXB annotations, in case you want to use both.
XMLBeans is also worth looking at.
Simple XML Serialization (simpleframework.org)
Another approach is Simple its far easier to use than JAXB, JiBX or other such tools. You simply annotate class and you can serialize any POJO to XML. Also, its probably the only framework that currently works on all known Java platforms, including Android, Google App Engine, any JDK 1.5+. For more information you can check out the Tutorial
There are also:
- java.beans.XMLEncoder/ -Decoder
- javax.xml.stream.XMLStreamWriter
But I recommend to use JAXB if you don't have a good reason to do otherwise.
You can look at castor and jibx as well.
Apache Digester is an option.Here is a tutorial for it.If JSON format is acceptable then you can use google-gson.
Castor allows generation of Java classes(decent implementations) straight from XSD schema (use case #1 above). Just be sure, in Android 2.1, not to use default android SAXParser. You'll get namespace errors. You do this by defining the parser to be, for example, Xerces (and the you add the required JARS), in core.properties . In android 2.2 it may be ok. Note that if you create an xmlcontext for the unmarsheler with xerces, it still won't work, as the mapping itself would be parsed with android's SAX. It must be done at core (top level properties file) so that even the mapping is parsed by xerces. finally - performance is as slow as you can expect... :( Good luck SM
精彩评论