Welcome to Java2XmlBinding (J2XB)
What is J2XB
The J2XB project aims at offering seamless Java and XML integration defined in Java. It offers the developer a tool to bind XML documents to Java types. What is special with J2XB is that no code Java generation is involved. J2XB transform the development of Java and XML binding from Java code generation to XML Schema generation.
The J2XB project is unique in that it aims at:
- Complete XML Schema generation from the Java code, including all Schema features.
- XML validations (Facets, such as minLength and patterns).
- Substitution Groups
- Full control of the XML document structure, including flattening Java structures and introducing grouping XML elements.
- Support all XML Simple types
- XML Union Types
- XML List Types
- Bind any Java object to XML.
- No requirement for inheriting from a common ancestor.
- No requirement to implement any interface.
- No requirement to have a default constructor.
- No requirement to even have a public constructor, if a factory is provided
- Full support for factories (static factories, instance factories)
- Support for Java built in collection types (Set, List, Array)
- Complete support for inheritence
- Mapped as Substitution Groups
- Complete and accurate XML Schema generation of the Substitution Groups
- Mapping of generic atomic properties (of type Object and Number) to XML Unions.
- Completely decoupled from Xml Parsers. Support for two parsers are available (Apache Axiom and Apache XmlBeans), but other parsers can be added.
Whats new in version 1.1
- New annotations MORegisterPropertyEditors and MORegisterPropertyEditor
- New annotation MOSeeAlso
- New annotation MOWebParam
- Default mapping for builtin Exception types, with optional mapping of stack trace
- Core Changes to support J2XB integration with Axis2 - Update multiple annotations to be applicable to method parameters (for web methods only)
- Numerous bug fixes
Getting Started
Binding Java classes to XML with J2XB is as simple as it can be - just annotate your classes with a few annotations and you are ready (to get the full benefit of J2XB you will need to add only annotations).
- First, download J2XB and include it in your classpath.
- Add annotations to your Java code. At the vary minimum, you should add the MOPersistentBean
annotation for every mapped class and the MOProperty
annotation for every Java property to map.
@MOPersistentBean(xmlName = "HelloWorld")
public class HelloWorld {
private String name;
private String type;
private int classID;
@MOProperty()
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@MOProperty()
public int getClassID() {
return classID;
}
public void setClassID(int classID) {
this.classID = classID;
}
@MOProperty()
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
- Create your binding model.
MoXmlBindingModel xmlBindingModel = new MoXmlBindingModel();
xmlBindingModel.addBean(HelloWorld.class);
xmlBindingModel.addBean(MyClass2.class);
- Generate the XML Schema (or Schemas).
xmlBindingModel.generateSchemas(new File("some folder"));
- Select the XML Parser to use. Currently J2XB supports Apache Axiom and Apache XmlBeans. You can use the Axiom persister
// using the Axiom persister
XmlPersister xmlPersister = new XmlAxiomPersister();
or the XmlBeans persister (which requires to pre-compile the generated schemas (in step [[4]] above) and including the generated classes in the classpath.
// using the XmlBeans persister
XmlPersister xmlPersister = new XmlBeansPersister();
- Write an intance to XML
HelloWorld myInstance = new HelloWorld();
xmlPersister.save(xmlBindingModel, myInstance, "c:/temp/myFile.xml");
- Read the instance from XML
HelloWorld myInstance = xmlPersister.load(xmlBindingModel, HelloWorld.class, "c:/temp/myFile.xml");
What next
Have a look at the documentation to utilize the full power of J2XB, Have a look at the examples, And try J2XB!
What is still to be done with J2XB
The J2XB project is at a mature stage and offers important benefits with the feature set it includes. However, like any software project, there is still a lot that can be done to improve this project. Some of the ideas that can be added to this project are:
- Ant task to generate XML Schema from source files.
- Overriding the annotations on a Java class with an external XML configuration file.
- Automatic handling of Bean cycles, where one bean points to another and the other back to the first.
- Alternate methods for Java property definitions (e.g. properties without getXXX and setXXX methods).
- Bean inheritence mapped as choice instead of Substitution Groups.
- Enhancing the plaggable simple types (MOPropertyEditor
)
- Support for the Map collection
- Complete support for contextual factories
- XML Schema to annotated Java classes code generator