Java XML Binding [closed]
Asked Answered
S

11

20

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.

Saddlebag answered 15/10, 2008 at 17:57 Comment(0)
S
8

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.

Sate answered 15/10, 2008 at 19:47 Comment(1)
+1 for JiBX. The big advantage I see is the way in which JiBX seems to be used. Although code generation is an option, there seems to be a lot of emphasis on writing you own mappings for your own data model. Anything that keeps me from dealing with generated classes makes me happy. I used JiBX for a few years and loved it. I cannot say the same for Castor. :)Incudes
W
14

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.

Warga answered 15/10, 2008 at 20:8 Comment(2)
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.Saddlebag
If you are familiar with XStream at least, Would you mind checking out my question about XMLDecoder versus XStream? #96559Militant
S
8

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.

Sate answered 15/10, 2008 at 19:47 Comment(1)
+1 for JiBX. The big advantage I see is the way in which JiBX seems to be used. Although code generation is an option, there seems to be a lot of emphasis on writing you own mappings for your own data model. Anything that keeps me from dealing with generated classes makes me happy. I used JiBX for a few years and loved it. I cannot say the same for Castor. :)Incudes
P
4

If you have an XSD for the XML, and you don't need to bind 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); 
Penalty answered 15/10, 2008 at 20:15 Comment(0)
M
3

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

Mulford answered 15/10, 2008 at 18:10 Comment(4)
Wow, that looks pretty slick!Misappropriate
Xstream is an xml serialisation technology, not a binding technology.Warga
Yes, the difference between serialization and binding is an important one.Militant
Check out: bdoughan.blogspot.com/2010/10/…Maser
T
2

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

Tome answered 15/10, 2008 at 18:28 Comment(0)
M
2

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.

Mattos answered 15/10, 2008 at 18:45 Comment(2)
Seems to have a number of good links here: https://mcmap.net/q/202750/-xml-serialization-in-java-closedMattos
Check out my blog for JAXB examples: bdoughan.blogspot.comMaser
C
1

I used castor 7 years ago -- it worked fairly well. used DTDs. Not many choices at that time.

In current projects, I've used
1) JAXB -- standards based, Reference implementation available, command line and ant tools available. latest version - 2.1.8 needs java 5+.
2) XStream -- for Soap unmarshalling -- needs Java 5+. Is not as fast and standards compliant as JAXB latest.

BR,
~A

Confessor answered 15/10, 2008 at 19:40 Comment(0)
A
1

Related: XML serialization in Java?

Aretha answered 15/10, 2008 at 23:39 Comment(0)
N
1

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.

Nimiety answered 28/10, 2008 at 21:27 Comment(0)
G
0

We use Castor. It suits our needs fairly well.

Grandparent answered 15/10, 2008 at 17:59 Comment(0)
O
0

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.

Osborne answered 25/2, 2009 at 0:37 Comment(1)
That study's from 2003 - anyone know of anything up to date?Arnulfo

© 2022 - 2024 — McMap. All rights reserved.