JAXB

Create Java Classes from a LIXI2 Schema

As a Java developer, the first thing you will want to do when you start working with the LIXI schema is generate Java classes from the XSD file. To save you some time (and get you on the real work as quickly as possible) here is a quick guide!

The framework we will be using to generate the Java classes is called JAXB. Before you begin you will need to install Eclipse IDE and install JAXB in Eclipse.

How to Create a JAXB Project

1) After installing JAXB in Eclipse, follow these steps to create a new JAXB Project:

1) Select File -> New -> Project
2) Choose JAXB Project from JAXB and click Next
3) Give Project Name (we will name the project 'LIXI') and click Next
4) On the next window, just Click Next
5) On the next window (titled JAXB Facets), 
        Choose "User Library" for JAXBimplementation. 
        Click "Download Library.." picture and choose EclipseLink 2.5.2 
        (or the higher version shown in the window) and click Next
        This will install the library.

2) Create a package to hold your LIXI schema XSD file:

3) Create a package to hold your Java classes:

Add a LIXI Schema and JAXB Bindings File to the Project

1) download the LIXI CAL 2.6.24 schema file (without annotations).

2) Drop the LIXI schema into the lixi.xsd package

You will need to add a JAXB Bindings file (.xjb) to the project. The bindings file is required because JAXB does not allow the name of the root element "Package" to be used as an element name elsewhere in the schema. The LIXI2 schema has more than one element named Package. We need to provide a unique name for each of the elements named Package found elsewhere in the schema.

3) Create a bindings file named LIXI_CAL-2_6_24-bindings.xjb and copy the following code into the file:

<?xml version="1.0" ?>
<jaxb:bindings  version="2.0" 
                xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
                xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                schemaLocation="LIXI-CAL-2_6_24.xsd" 
                node="/xs:schema">
  <jaxb:bindings>
    <jaxb:globalBindings typesafeEnumMaxMembers="2048"/>
  </jaxb:bindings>
  <jaxb:bindings node="//xs:element[@name='DepositAccountDetails']/xs:complexType/xs:sequence/xs:element[@name='Package']/xs:complexType">
    <jaxb:class name="DADPackage"/>
  </jaxb:bindings>
  <jaxb:bindings node="//xs:element[@name='LoanDetails']/xs:complexType/xs:sequence/xs:element[@name='Package']/xs:complexType">
    <jaxb:class name="LDPackage"/>
  </jaxb:bindings>
</jaxb:bindings>

2) Copy your LIXI bindings file into the lixi.xsd package:

How to Generate Classes from Schema

1) Right click on your package -> New -> Other -> JAXB -> JAXB Classes from Schema.

2) Select your Java project - > Next

3) Select your Schema -> Next

4) Select your Java classes package and bindings file -> Next

5) Select "Treat Input as XML schema" -> Finish

Done!!! In the terminal you should see output starting with:

parsing a schema...
compiling a schema...
lixi\javaclasses\AbsLendingPurposeCodeList.java
lixi\javaclasses\AccountHoldingList.java
lixi\javaclasses\AccountStatusList.java
lixi\javaclasses\AddressType.java
lixi\javaclasses\AddressTypeList.java
lixi\javaclasses\AggregatedTransactionsCategoryTypeList.java
...

And in your lixi.javaclasses package you should see the list of LIXI Java classes:

Using your LIXI Java Classes

1) Create a new package in your project. I have created the class "lixi.project"

2) Create a new class in your project, I have create one named "LIXIApplication"

3) Add the import statement: import lixi.javaclasses.Package;

package lixi.project;

import lixi.javaclasses.Package;

public class LIXIApplication {
    public static void main(String[] args) {
        Package lixipackage = new Package();
        Package.Publisher publisher = new Package.Publisher();
        lixiPackage.setPublisher(publisher);
        publisher.setCompanyName("LIXI");
        publisher.setContactName("John Matthews");
        publisher.setEmail("john@lixi.org.au");
    }
}

Related Posts...

For a demonstration of how to create the same JAXB classes using Maven rather than JAXB in Eclipse see JAXB-Demo - To view this project you will need to be registered with an account in LIXILab .

We've published similar blog posts showing how to generate Python classes here and C#Classes here.


Written by:
John Matthews, LIXI Technical Lead
First Published: February 6, 2020