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:
Suppose real=1.0, imaginary=1.0, I would like to see the following XML:
More than that, suppose I had this:
I would want to see This:
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:
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.
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:
<EvenMoreComplexNumber>
<firstComplex real='1.0' imaginary='1.0' />
<secondComplex real='2.0' imaginary='2.0' />
</EvenMoreComplexNumber>
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:
@Root
class EvenMoreComplexNumber {
@InnerElement
public ComplexNumber firstComplex;
@InnerElement
public ComplexNumber secondComplex;
}
@Element
class ComplexNumber {
@Attribute
public double real;
@Attribute
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.
Comments (4)
Also, I hope that you don't think it's ok to have public member variables.
Regarding your second question: Obviously not. The framework itself supports JavaBeans and only JavaBeans: getters, setters and a default constructor are requirements.
Attributes are best left to indicating modifiers of the member, not the data of the members themselves.
Randy
[email protected]
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.