| Sign In/My Account | View Cart |
The State of JAXB: Availability, Suitability, Analysis, and Architecture
Pages: 1, 2, 3, 4
When our team looked into a solution for processing XML messages, JAXB seemed like an obvious choice, as it is part of the Sun J2EE infrastructure. XSDs were not an issue, as we already have XSDs for the XML messages that we deal with. These XSDs are fairly complex. We are quite pleased with the JAXB handling of our XSDs. JAXB also seems to process the XML messages quite efficiently. JAXB is also found to be quite reliable.
XSDs being such an integral assumption of JAXB, the validation of data structures against an XSD is quite strong and a useful element in the solution. The documentation of JAXB also goes on to say that one can use the RelaxNG syntax for schemas via an available extension.
Although the team is quite pleased with JAXB, I see some issues with JAXB for broader architectures. At the outset, JAXB requires a schema document (at least in the current release). This means you cannot take an XML document and try to map it into a similarly structured Java class. More importantly, these Java classes need to be generated by JAXB. This is because the serialization and unserialization are accomplished through JAXB's hidden implementation classes and not through reflection. This approach might be more efficient, but from a maintenance and expediency perspective, this solution doesn't fare very well.
JAXB prides itself on allowing for multiple implementations. I am not sure if this requirement hasn't stretched JAXB to be more complex than it needs to be. This idea resulted in interfaces and implementations for the binding generated classes. This makes it hard to change the generated classes — they're usually not touchable for any modifications. So the final authority is the XSDs and not the java classes. Another consequence of this approach is that the generated classes are more numerous and more complex compared to .NET's binding approach.
Due to the nature of the interfaces, one cannot new content objects:
you have to use a factory. This can be very tedious, as your content hierarchy
involves getting new objects all the time. For every object creation you have to go to the factory.
The validations and extensions to the default behavior are a bit complex. For example the inline annotations and external annotation files for customizing binding are a bit more complex than .NET. Given that Java 1.3 does not have support for meta attributes this may be the only reasonable solution.
Architecturally, it is also worth noting the sentiments of others on the subject of JAXB. The primary complaint with JAXB seems to be the mandatory reliance on the W3C schema in the current release. For instance, in .NET, you can take an arbitrary C# object and serialize it as XML. The object doesn't have to be generated from a schema. Even though the original object may have been generated from the schema, you can tweak the object, add a couple of fields, and serialize it as XML.
The second problem is the nature of the code generated by JAXB. JAXB
generates interfaces representing the content (domain) objects. This
means when it is time to create the object tree, you can't just
instantiate these objects. You have to use the ObjectFactory created by
JAXB to create each new object. This can be a bit tedious compared to
creating other objects with a typical new.
As I said, JAXB generates interfaces that you can't change or touch
-- if you do, then the implementations will be out of whack. It is
almost as if these generated classes are untouchable. Any necessary changes
have to be made to the XSDs, and changing XSDs is not
necessarily child's play. Perhaps with the help of xmlspy this might be easy enough. But changes to XSDs can result in different styles of Java code generation. For simple tweaks, this seems to be a
heavy-handed approach. That said, ne can also argue that the JAXB approach is
better, as the "truth" is always maintained in one place: the XSD file.
There is one more issue with the generated interfaces. It might be minor, but it's worth mentioning: because of the get/set methods of the interfaces, the interface definitions tend to be verbose, clouding the structure of the data (not to mention bloating the size of the generated code). In contrast, .NET produces classes with public properties.
Although one could argue with the sanity of public properties, the approach
has a practical advantage to it: clarity.
Although the architecture of interfaces, implementations, and multiple providers is seen in general as an advantage the resulting solution seem to be unnecessarily complex for general usage. Perhaps this is a difficult point to argue as perhaps JAXB might come out to be wiser in the long run for the provided flexibility.
In general, the XML binding infrastructure in the Java space seem to be much more diversified and involved compared to .NET's approach. Having worked with both .NET's XML binding and JAXB's binding, .NET seem to be lot more easier and cleaner for the simpler and straightforward cases. Nevertheless, I am not sure how they fare in cases with complex and more flexible requirements.
JAXB and Castor seemed to be the primary contenders in the Java space until recently. With the recent release and inclusion of JAXB into the web services pack, I believe more and more people are going to be using JAXB. Also once one accepts the fact that they have to work with XSDs to work with JAXB (for now) one can always find workarounds for existing Java classes.
As pointed out in the "Experiences with JAXB" section, it seems to be stable, reliable and usable for day-to-day operations. A few of my colleagues, Sayed Hashimi and David Goyette, have used JAXB extensively for XML message processing and also for configuration files. Their feedback is that both are quite thrilled with the results and their lives are lot easier now with JAXB, compared to their earlier use of a SAX parser for the same project.
Nevertheless, it pays to know the following while working with JAXB:
xmlspy to work with XML and XSDSatya Komatineni is the CTO at Indent, Inc. and the author of Aspire, an open source web development RAD tool for J2EE/XML.
Return to ONJava.com.
Showing messages 1 through 11 of 11.
Both Castor (and now JAXB) are proven tools - if you read the Castor mailing list - there isn't much you couldn't do with Castor, and if you did find a scenario, the developers were quick to have a workaround or fix.
That's the reason for the extra complexity of these tools (JAXB was in beta for over 3 years), but what you get is incredible power such as JAXB's support for multiple implementations. Downright impressive.
Sure, .NET's version might be simpler out of the box, but no doubt you'll run into a wall quickly, just like the rest of that toolset.
John,
Thanks for a very informative post. This is the beauty and bane of java at the moment. Very inventive solutions are there all around but difficult to standardize on in an IT environment. Unfortunately at the start of the article I haven't looked into XMLEncoder. As some posters are suggesting it may not be a bad idea to look into it for simple xml documents and cases where no one is yelling out loud for an XSD. For cases where XSD is available I felt JAXB is very good over all. I have looked at Castor only briefly as JAXB is part of the standard. Unfortunately I can't answer convincingly which of the alternatives are better where XSD is not available or cumbersome to maintain. Looks like you are on the right track to look at the right tools. Perhaps you can publish your results :-) and your experiences.
Thanks
Satya
Thanks for the input. I haven't looked into XMLEncoder and Decoder. Perhaps as you have suggested they could be use for light weight uses which in my mind are quite a few. Again thanks for the pointers. Nevertheless it is interesting that we separate these XML data binding needs as serialization needs and data exchange needs although what is produced is XML in both cases. I think XML serialization is much more loose and lossy and problem domain specific and can cover more than object persistence. I don't think lumping this kind of serialization for XML generation doesn't suit the problem space either. We need a ground between this and JAXB. It would have been nice to see such a facility as part of JAXB. In fact there are suggestions in the JAXB architecture that perhaps it is possible to provide JAXB implementations without requiring an XSD or an alternative schema.
Thanks
Satya
Mike,
Thanks for the comments. XML being structured data, XML's applications go beyond "cross platform data transfer". XML is being used in quite unexpected domains including inside of a given program space never crossing the boundaries. Take configuration files. The data never crosses the process boundary. One can argue that configuration files can benefit from XSDs. This is a choice and doesn't have to be mandatory.
Another example is data transformation where data is read from a database and transformed to become an html page in a web environment. The data can be retrieved either as an object or dom depending on the eventual transformation. Again although one can define an XSD for it, lot of times one doesn't need it. Having this choice simplifies the development effort.
Take another case where one has to send an email in html format on the back end based on certain program data. Here one should be able to populate a corresponding complex object and apply xslt to the resulting xml. Again one can publish XSD, but I don't think the value of it is as significant as the case you have quoted.
Databases are becoming increasingly XML aware. For example, with reasonable approximations you can send a large XML to a database and have the database automatically do multiple inserts for the entire data one time. In this scenario one can create java objects in memory to match the schema and insert the object tree directly into the database one time. Again having XSDs for all possible cases will be a proliferation of files not to mention the factory generated code in your applications.
Same with work flow applications where multiple components work on a single data set as it passes through a work flow stream. Defining XSDs, in my opinion, is going to make the maintenance process quite cumbersome as there are so many aspects of the same data structure needs to be maintained.
I also think that XSD is one way of defining structured data. Object languages like Java and C# is another way to define structured data. Both have their strengths and weaknesses. I have felt that having XSD optional would have been nice. Some of the postings here have suggested other means to serialize java objects as xml if one doesn't need the XSD support. Perhaps not a bad idea.
These aspects can not be new and I am sure well discussed in the JCP, and they have good reasons to base JAXB on XSD to cover the more common cases and still be an efficient implementation. But that leaves a gap for cases where XSDs are not that necessary and the programmer has to seek alternatives.
Thanks
Satya
I'm also wondering if the author done much development of enterprise applications or if he understands his target audience (whom he claims are 'senior architects').
He writes "this approach has the disadvantage of requiring knowledge of Ant...". Every project that I have ever worked on used ant for builds and deployments. I would hope that anyone with the title of senior architect or senior developer for java based applications has at least a working, if not an expert understanding of Ant.
He also complains about JAXB creating interfaces instead of concrete classes. Again, anyone familiar with sofware engineering, especially in OO languages like java will tell you "program to the interface". There is no reason for the client code to know about the concrete implementations. You can easily externalize the object creation to a delgate factory to allow changing JAXB implementors very easy instead of hardcoding calls to ObjectFactory everwhere.
Also, his 'discomfort' with JAXB not having constructors for required attributes is curious. As JAXB objects are implicitly java beans, anything but a no-arg constructor is pointless if you want to interact with your beans reflectively and would 'muddy' the waters, not to mention break the idea of programming to the interface.
FWIW, I've used JAXB on a couple of projects and there are some serious limitations with Sun's implementation but, in general, it does solve a common problem that we've encountered and is a large step in the right direction.