Example code

Here's a set of classes to be used as an example for the binding generation tool, all from the example package:

public class Customer {
    private Name name;
    private Address address;
    private Order[] orders;

public class Name {
    private String firstName;
    private String lastName;

public class Address {
    private String street;
    private String city;
    private String state;
    private Integer zip;

import java.math.BigDecimal;
import java.sql.Date;

public class Order {
    private Date date;
    private BigDecimal amount;

Running the binding generator

I'll assume that the above set of classes have been compiled into the current directory, and that the jibx-genbinding.jar has been copied into the lib directory of the JiBX installation located at /home/dennis/jibx. Then to generate a default binding for these classes I just need to run the following:

java -jar /home/dennis/jibx/lib/jibx-genbinding.jar example.Customer example.Order

Note that you don't need to specify every class to be included in the binding definition, only those classes which should be bound using <mapping> elements. In this case the example.Customer class is the root object of the data structure, so it needs to be specified. The example.Order class also needs to be specified, since it's referenced as the elements of an array. The other classes which need to be included in the binding are referenced directly from the example.Customer class, so these do not need to be specified.

Here's the generated binding:

<?xml version="1.0" encoding="UTF-8"?>
<binding value-style="attribute">
  <mapping class="example.Customer" name="customer">
    <structure field="name" usage="optional" name="name">
      <value style="element" name="first-name" field="firstName" usage="optional"/>
      <value style="element" name="last-name" field="lastName" usage="optional"/>
    <structure field="address" usage="optional" name="address">
      <value style="element" name="street" field="street" usage="optional"/>
      <value style="element" name="city" field="city" usage="optional"/>
      <value style="element" name="state" field="state" usage="optional"/>
      <value name="zip" field="zip" usage="optional"/>
    <collection field="orders" usage="optional"/>
  <mapping class="example.Order" name="order">
    <value name="date" field="date" usage="optional"/>
    <value name="amount" field="amount" usage="optional"/>

Binding generation problems

Problems and potential problems in the binding generation are reported in the console output from the binding generator tool. For example, suppose the array of example.Order references is instead a java.util.ArrayList:

import java.util.ArrayList;

public class Customer {
    private Name name;
    private Address address;
    private ArrayList orders;

Then running the binding generation tool gives a warning message:

Warning: field orders requires mapped implementation of item classes

Similar warning or error messages may result from other types of situations which cannot be handled by the binding generator. The command line options supported by the binding generator can be used to handle many of these situations by supplying additional instructions to the binding generator.