Alignment: Chaotic Java

Saturday, December 10, 2005

XML, Annotations and a simpler life

I've just posted a request to SourceForge so I could open-source my X2J framework.

It's a little something I've hacked up lately that helps me generate XML files from POJOs, and POJO instances from XML files, with validation but Without pesky XSD files.

To illustrate, suppose I have the following class:

class ComplexNumber {
public real;
public imaginary;

Suppose real=1.0, imaginary=1.0, I would like to see the following XML:

<ComplexNumber real='1.0' imaginary='1.0' />

More than that, suppose I had this:

class EvenMoreComplexNumber {
public ComplexNumber firstComplex;
public ComplexNumber secondComplex;

I would want to see This:

<firstComplex real='1.0' imaginary='1.0' />
<secondComplex real='2.0' imaginary='2.0' />

Sure, I could use JAXB for that - But who has the nerves to use this cumbersum library and crazy XSD files?

This is my solution:

class EvenMoreComplexNumber {
public ComplexNumber firstComplex;

public ComplexNumber secondComplex;

class ComplexNumber {
public double real;

public double imaginary;

The X2J framework can validate arrays and limitations and can validate mandatory items. It will be able to generate XSD files and generate the annotated classes from already existing XSD files. Just give me some time. :-)

Just a simple note, before someone puts me up on the cross: I called it X2J just out of the top of my head, "Xml To Java" kind of thing. I wasn't aware when I requested the project name that someone took it already. I hope it's not much of a problem. Further, I hope I won't be allowed with this name and I could choose a different one.


At 12/11/2005 05:23:00 PM, Anonymous Michael Slattery said...

I like the idea of using annotations to help with the XML generation. However, hasn't XML-Java binding been done to death? What about XStream?

Also, I hope that you don't think it's ok to have public member variables.

At 12/11/2005 06:13:00 PM, Blogger Avah said...

I imagined there would a framework that does this, but wanted to create this framework anyway. The difference is the annotations: XStream is used to serialize objects to and from XML files. X2J is used to replace XSD with all the validations it offers.

Regarding your second question: Obviously not. The framework itself supports JavaBeans and only JavaBeans: getters, setters and a default constructor are requirements.

At 12/12/2005 10:14:00 AM, Anonymous Anonymous said...

I think that you'll find using attributes to store your members to be a bad idea. For members that are primitives (int, float, bool), it works OK. But more complex types can't be represented this way, and this sets up a situation where some types are handled via attributes, while others are expressed as nested elements. And it appears (to the outside) to be arbitrary in choice.

Attributes are best left to indicating modifiers of the member, not the data of the members themselves.


At 12/12/2005 11:00:00 AM, Blogger Avah said...


One of us is not understanding the other, I suppose. Where did I use annotations to store the data? I use the annotations to tell the framework how the property would be described in the XML - As an element, attribute, an array of elements, etc.

Nesting is available, as the example I've given.


Post a Comment

Links to this post:

Create a Link

<< Home