28

What 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 available for converting Java to XML ?

Update: Different suggested approaches are:

  1. javax.xml.bind.Marshaller and javax.xml.bind.Unmarshaller
  2. XStream
  3. XMLBean
  4. JAXB
  5. Castor
  6. JIBX
  7. 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 ?

4
  • I used XStream in the past. It was relatively un-painful, and flexible -- but the flexibility comes from the ability to extend/add different visitors in code itself vs. some funky XML/XSD or DSL. Commented Nov 20, 2010 at 5:41
  • So how does XStream stack up with JAXB ? Commented Nov 20, 2010 at 13:27
  • Here is my comparison of JAXB and XStream: bdoughan.blogspot.com/2010/10/… as I have previously admitted I do lead a JAXB implementation but I believe the comparison is fair. I did a similar comparison between JAXB and Simple XML, and the Simple XML site lists it in their articles section. Commented Nov 21, 2010 at 13:55
  • Another approach is to use the extensions in the various JAXB implementations: Metro (the RI), MOXy (I'm the tech lead), JaxMe. For example MOXy offers XPath based mapping: bdoughan.blogspot.com/2010/09/… Commented Nov 21, 2010 at 14:02

9 Answers 9

32

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:

  • 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:

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).

Sign up to request clarification or add additional context in comments.

5 Comments

Blaise is a JAXB team lead, so perhaps he's not the most objective opinion.
Yes it does. Your down vote was unwarranted. And being a JAXB lead certainly makes you an authority on your technology, but it's not the only alternative out there. XStream would be just as valid. Personally, I like it far better than yours.
That's your opinion, and you're entitled to it. The OP asked for other approaches, and I gave one.
I have undone the downvote. Here is my take on JAXB and XStream: bdoughan.blogspot.com/2010/10/…
@Blaise: Thank you very much for this detailed answer, appreciate your inputs on this. I was looking for something similar.
11

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.

8 Comments

Fewer dependencies, more control. Some libraries that serialize Java to XML and back don't do good things. Check out how they handle collections sometime and see what I mean. Marshaller and Unmarshaller implementations aren't that hard to write if you have simple classes that aren't deeply nested, and you only have to do it once. I should point out that you're listed as a JAXB team lead, so perhaps your bias is showing, Blaise.
+1 to counter mindless downvote.
Fine, but that doesn't warrant a downvote because you disagree. What I said isn't wrong, you just don't happen to like it. The original question asked for alternatives, and I provided one with a good reason for choosing it. You should cite your background so it's clear to people.
I believe JAXB was only folded into Java with JDK 6; earlier versions would have required additional JAR dependencies - hence my comment.
@duffymo: Thanks for sharing this information, also your comments are helpful too.
|
2

Some alternatives include:-

However, one significant advantage of going with JAXB is it ships with the Java distrubtion.

1 Comment

Check out my comparison of JAXB and XStream: bdoughan.blogspot.com/2010/10/…
1

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.

3 Comments

+1 for Jackson - I like it very much. Just started using it recently.
Jackson is only for JSON right and not for XML, is that correct ?
Right, Jackson translates Java to JSON and back. It doesn't answer the question per se, but it does put the alternative of using JSON on the table. It'll be far fewer bytes than XML.
1

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

2 Comments

With JAXB you also simply annotate your classes in order to serialize any POJO to XML. The amount of work is similar see: bdoughan.blogspot.com/2010/10/…. JAXB is also much easier to use with standard Web Service frameworks such as JAX-WS (SOAP) and JAX-RS (REST).
Its a 5 minute job to integrate with JAX-RS, I doubt JAXB is going to be any easier to use here. Also features such as constructor injection, templating, and many more seem missing in JAXB, as is support for Android.
0

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.

Comments

0

You can look at castor and jibx as well.

Comments

0

Apache Digester is an option.Here is a tutorial for it.If JSON format is acceptable then you can use google-gson.

4 Comments

Will Apache Digester useful for converting Java to XML, I was going through the document and it appears that it is used for XML to Java ? Is that correct ?
@Rachel:Yes it used for converting Java objects to xml.
I was going through the reviews regarding Apache Digester and it appears that is it not the best approach, is that true or you have some different experience using it ?
I have never used it.But from what i have read,its performance is not that good.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.