views:

1297

answers:

12
+8  Q: 

Java XML Binding

What are you using for binding XML to Java? JAXB, Castor, and XMLBeans are some of the available choices. The comparisons that I've seen are all three or four years old. I'm open to other suggestions. Marshalling / unmarshalling performance and ease of use are of particular interest.

Clarification: I'd like to see not just what framework you use, but your reasoning for using one over the others.

A: 

We use Castor. It suits our needs fairly well.

Adam Hawkes
+2  A: 

We use xstream. Marshalling / unmarshalling is trivial. See their tutorial for examples.

Steve K
Wow, that looks pretty slick!
Michael Myers
Xstream is an xml serialisation technology, not a binding technology.
johnstok
Yes, the difference between serialization and binding is an important one.
erickson
+1  A: 

Jibx is what is used around here. It is very fast, but the bindings can be a little tricky. However, it is especially useful if you have XML schemas describing your domain objects, as it really maps well to XSD (there's even a beta tool XSD2Jibx which can take XSDs and create stub domain classes and mappings, which you can then take and coax to fit your existing domain model).

It manipulates bytecode, so it must be run after the initial compilation of the Java .class files. You can use the Maven plugin for it, or just use it directly (the Eclipse plugin didn't seem to work for me).

MetroidFan2002
+1  A: 

I've used Jaxb with varying success. At the time (a couple of years back) the overall documentation was lackluster and the basic usage documentation (including where to download implementations) was difficult to find or varied.

The parser which wrote the Java classes was quite good with little discrepancy against the original XSD (though I think it had problems supporting abstract XML elements).

I haven't used it since, but I have an upcoming project which will require just such a framework and I will be interested to know how anyone else fairs with the above.

Spencer K
+1  A: 
anjanb
+3  A: 

JiBX. Previously I used Castor XML, but JiBX proved to be significantly better, particularly in terms of performance (a straight port of some application code from Castor XML to JiBX made it 9x faster). I also found the mapping format for JiBX to be more elegant than Castor's.

JiBX achieves its performance by using post-compilation bytecode manipulation rather than the reflection approach adopted by Castor. This has the advantage that it places fewer demands on the way that you write your mapped classes. There is no need for getters, setters and no-arg constructors just to satisfy the tools. Most of the time you can write the class without considering mapping issues and then map it without modifications.

Dan Dyer
+5  A: 

If you want to make an informed decision you need to be clear why you are translating between XML and java objects. The reason being that the different technologies in this space try to solve different problems. The different tools fall into two categories:

  1. XML data binding - refers to the process of representing the information in an XML document as an object in computer memory. Typically, this means defining an XSD and generating a java source code equivalent. Interop between different languages is top priority (hence the use of XSD) - most typically for the implementation of SOAP-based web services.
  2. XML serialisation - refers to writing out a graph of in memory objects to a stream, so that it can be reconstituted somewhere or sometime else. You write the java classes by hand; the xml representation is of secondary importance. Also, the need for performance is often greater and the need for interoperation with other languages such as .net is often lower.

For xml serialisation, Xstream is hard to beat. JAXB is the standard for XML binding.

In either case, if you are using J2EE you'll need to pay careful attention to classes retrieved from JPA since class proxies and persistence specific collection types can confuse binding / serialization tools.

johnstok
Thanks for your input. I'm looking for a binding solution. This is for a RESTful web service with an existing XSD. We've been using Castor for a number of years. I'd like to know the reasons that JAXB (or whatever) is better / worse.
Paul Croarkin
If you are familiar with XStream at least, Would you mind checking out my question about XMLDecoder versus XStream?http://stackoverflow.com/questions/96059/what-are-the-relative-advantages-of-xmlencoder-and-xstream
erickson
+3  A: 

If you have an XSD for the XML, and you don't need to bing the data to an existing set of classes, then I really like XMLBeans. Basically, it works like this:

  • Compile XSD
  • Use generated java classes to read/write documents conforming to this schema

Binding an XML document to the generated classes is as simple as:

EmployeesDocument empDoc = EmployeesDocument.Factory.parse(xmlFile);
Don
A: 

Related: XML serialization in Java?

eed3si9n
A: 

XmlBeans is a good choice especially if you have 'broken' XSD/WSDL files.

Don mentioned

EmployeesDocument empDoc = EmployeesDocument.Factory.parse(xmlFile);

..but it can also take a Node, or a File, or just about any source.

No fighting with namespaces, traverse to the object you want to unmarshall, and Factory.parse it.

Wish I had found it 2 weeks ago.

Cog
A: 

I was wondering exactly the same question, and finally I found this performance tests made by IBM. http://www.ibm.com/developerworks/library/x-databdopt2/. JiBX is my choice I guess, hehe.

victor hugo
That study's from 2003 - anyone know of anything up to date?
Tom
A: 

This article may also help you ....

http://onjava.com/pub/a/onjava/2007/09/07/schema-less-java-xml-data-binding-with-vtd-xml.html

vtd-xml-author
@jzhang: this borders on being spam. You're answering a question that has already been answered months ago, and all you do is post the link to an article that makes your product look good. It's a little shady.
John Saunders
did you see all the teachers badges and all the points voted up? is it your opinion or maybe you should let the one who ask questions decide? I am trying to help people solve problems... it seems like you go out of your way to forbid people from a solution that will help them do their work... this hardly is consistent with the purpose of this website in the first place...
vtd-xml-author
What are you talking about? What teachers badges? What upvotes? And, yes, go read the faq, and you'll see that the community monitors, edits, and in general, runs this site.
John Saunders
If you're for real, then you should start a discussion at meta.stackoverflow.com, and explain why you are not the same as the spammer who has been posting the exact same link for the past month or so. Did you not get my email? I tried to inform you of him.
John Saunders