Title Image

Don Xml's Grok This

The home of Don Demsak
Welcome to Don Xml's Grok This Sign in | Help
in Search

This Blog

Syndication

Site Sponsors

DonXml's All Things Techie

XStream – A More .Net Like XML Serialization Library for Java

I’ve got a confession to make.  My current client is a Java shop, not a .Net shop.  Now, I’m “only” doing XML work here, so I don’t really have to do much Java coding, but knowing how the platform works is very important to creating an easy to use XML architecture.  One of the first gotchas I ran across is the poor support for XML serialization in the Java framework.  It isn’t nearly as advanced as the .Net framework when it comes to XML serialization, and attribute based programming.  To fill this niche, there are a bunch of open source libraries out there.  I’ve looked at a bunch of them, but since most of the Java coders out there haven’t taken a look at the .Net framework, they haven’t taken the time to learn how .Net does it, and adapt it (and/or improve it) for Java.  The closest library I’ve come across to the .Net style of XML serialization is XStream (thanks to this Xml.com article).  It does use reflection to help serialize the Java Objects (like in .Net), but the major thing it is lacking is the ability to control the serialization thru attributes (which .Net does do).  Instead, you have to add this information to your procedural code as settings via the alias method.

Now if I can only get an open source project going to implement an XPathNavigator-like library on the Java framework, maybe we can get some synergy between the Java and .Net XML developers.

Published Tuesday, August 24, 2004 8:16 AM by donxml
Filed under: ,

Comments

oleg@tkachenko.com (Oleg Tkachenko) said:

Yeah, great library. Too bad I have to use Axis for serialization :(
August 24, 2004 8:54 AM

Joe Walnes said:

Hi DonXML! Thanks for the good words.


Re: Using attributes

The design of XStream allows you to plug in your own strategies for things like this.

For your specific needs, you could create your own implementation of the ClassMapper interface which can be passed into the XStream constructor.

The easiest way to do this would be do subclass DefaultClassMapper, override the lookupName() and lookupType() methods and delegate to the attributes. Java 5.0 gives you annotations, and on earlier JDKs you can use QDox <http://qdox.codehaus.org/>.


Re: XPath

For XPathNavigation, you should also check out Jaxen <http://jaxen.codehaus.org/>. As well as allowing XPath navigation on XML documents, you can wrap it around your own datastructures so you could use it across things like object models or databases.


Re: .NET XML Serialization

There has been a lot of interest in porting XStream to .NET due as an alternative to the XML serialization that comes as standard. The main reason for this is that XStream produces much cleaner XML without configuration (particularly when it comes to collections) and it does not force you to have to add properties to classes (which violates basic laws of encapsulation).

Would you be interested in helping the project?

August 24, 2004 10:59 AM
Anonymous comments are disabled

About donxml

I’m an independent consultant, specializing in .Net solutions architecture, based out of New Jersey who also doubles as an evangelist for XML, Domain Driven Design, enterprise architecture and .Net. I do not work for Microsoft, the W3C or any other big company that you may know of (at least not yet). I’ve been an indie for over ten years, and although I’ve been tempted a couple times to take a job with companies like Microsoft, I’ve haven’t found something better than my current situation. I work mostly with the large pharmaceuticals that are based here in New Jersey, and usually find myself on long term contracts. Definitely not the prototypical indie consultant, but it lets me dedicate time to my non-income generating activities like the developer community stuff, plus financing open source projects like XPathmania and MVP-XML. If you would like to talk to me about doing some contract work, just contact me via the contact page. My rates vary widely, depending on lots of different variables, but mostly distance from Jersey, and type of work. Plus, I’ve been known to donate some of my code for various projects.
Powered by Community Server, by Telligent Systems