These pages about different languages / apis / best practices were mostly jotted down quckily and rarely corrected afterwards.
The languages / apis / best practices may have changed over time (e.g. the facebook api being a prime example), so what was documented as a good way to do something at the time might be outdated when you read it (some pages here are over 15 years old).
Just as a reminder.

Jibx: mapping xml to java

just random scribblings I made when developing with jibx

Used for mapping XML to Java objects.

Note: since jibx modifed the class file, it must have access to the .class file (i.e. the file can NOT be packed in a jar file)
(see note below regarding useing setter/getter instead to avoid that jibx modify the class file)

export JIBX="/home/dilbert/temp/t2/jibx/lib"
java -cp ".:importer-0.0.jar:${JIBX}/jibx-run.jar:${JIBX}/jibx-bind.jar:${JIBX}/xpp3.jar:${JIBX}/bcel.jar:test.jar" org.jibx.binding.Compile ../test/com/example/binddef/person1Binddef.xml


Use the attribute set-method, get-method instead of field.

Supposedly, that way JIBX does NOT need to modify the compiled class, it just verifies that the settters/getters exist in the class. However, for me it stills edit the compiled class...


Person inherits from MediaObject.java

  <mapping ordered="true" abstract="true" class="com.example.app.content.model.MediaObject">
 	   <value name="firstname" field="headline" />
 	   <value name="lastname" field="text" />

  <include path="mediaobject_binddef.xml"/>
  <mapping ordered="true" name="customer" class="com.example.app.content.model.Person" extends="com.example.app.content.model.MediaObject">
 	<structure map-as="com.example.app.content.model.MediaObject" />
 	   <value name="pppp" field="name" />

Mailing list



<sometag><customer id=\"42">"
+ "<firstname>AName11</firstname>"
+ "<lastname>LastName</lastname>" + "<pppp>oooo</pppp>"
+ "</customer></sometag>

    <include path="mediaobject_binddef.xml" />

    <mapping ordered="true" name="sometag"
 	   <structure name="customer">
 		   <value style="attribute" name="id" field="someid" />
 			   map-as="com.example.app.content.model.MediaObject" />
 		   <value name="pppp" field="name" />

Mapping - Custom classes

<structure name="place_of_birth" usage="optional">
   <structure name="geographic_item" field="placeOfBirth">
       <value name="id" field="myId" usage="optional" />
       <value name="name" field="name" usage="optional" />

  Geograhpy placeOfBirth;

Geography {
  String myId;
  String name;

Mapping - Date

Within the <mapping tag>
 	   <format label="Date_dd.mm.yyyy" type="java.util.Date"
 		   deserializer="com.example.app.util.JibxSerializer.deserializeGermanDate" />

<value name="date_of_birth" field="dateOfBirth"
 			   usage="optional" format="Date_dd.mm.yyyy"/>

public class JibxSerializer {

    private static SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy");

    public static String serializeGermanDate(java.util.Date date) {
 		   return null;
 	   return sdf.format(date);

    public static java.util.Date deserializeGermanDate(String date)
 		   throws ParseException {
 		   return null;
 	   return sdf.parse(date);

Mapping - java.sql.Time

The problem:
I am reading in some xml where I have a time that I map to an
attribute of type java.sql.Time.
The problem is that the input time will be stored with an additional
hour in the object,
e.g. 23:45 in the xml will be 00:45 in the object
13:15 in xml will be 14:15 in the object

in the binding:
<value name="timeSent" field="timeCreated"/>

in the class:
private Time timeCreated;

my input xml:

after parsing the xml, the object contains the time 15:42:00.

The solution:
    <format label="TIME_serializer" type="java.sql.Time"
 	   deserializer="com.example.app.util.JibxSerializer.deserializeTime" />

 	   <value name="timeSent" field="timeCreated" usage="optional" format="TIME_serializer"/>

    private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    public static String serializeTime(java.sql.Time time) {
 	   if (time == null)
 		   return null;
 	   return timeFormat.format(time);

    public static java.sql.Time deserializeTime(String time)
 		   throws ParseException {
 	   if (time == null)
 		   return null;

 	   Date date = timeFormat.parse(time);
        Time t = new Time(date.getTime());
  	   return t;

Mapping - collections

nationalites is abstract List, so we must say which factory to use when instantiating objects
<structure name="nationality" usage="optional">
  <collection field="nationalities" factory="org.jibx.runtime.Utility.arrayListFactory">
    <structure name="geographic_item" type="com.application.content.model.Geography">
       <value name="id" field="someId" usage="optional" />
       <value name="name" field="name" usage="optional" />
Inner classes

<structure name="position" type="com.wahtever.content.model.Person$Position">
  <value name="name" field="name" usage="optional" />

Multiple bindings

give the name of the binding to use to the getFactory method (the name is the same as the file name as default)
m_bindingFactory = BindingDirectory.getFactory("chunk_person_binddef", mclas);

Ignore elements in the xml

<structure name="element_name_to_ignore" usage="optional"/>

How to use in unit test

    private static final String MY_CLASS = "com.example.app.test.mock.Person";

    private static final IBindingFactory m_bindingFactory;

    static {
 	   try {

 		   // set paths to be used for loading referenced classes
 		   URL[] urls = Loader.getClassPaths();
 		   String[] paths = new String[urls.length];
 		   for (int i = 0; i < urls.length; i++) {
 			   paths[i] = urls[i].getFile();

 		   // find the binding definition
 		   ClassLoader loader = OurClassTest.class.getClassLoader();
 		   InputStream is = loader.getResourceAsStream("com/example/app/binddef/person1Binddef.xml");
 		   if (is == null) {
 			   throw new RuntimeException("binding definition not found");

 		   // process the binding
 		   BindingDefinition def = Utility.loadBinding("person1Binddef.xml",
 				   "binding", is, null, true);

 		   // output the modified class files

 		   // look up the mapped class and associated binding factory
 		   Class mclas = Class.forName(MY_CLASS);
 		   m_bindingFactory = BindingDirectory.getFactory(mclas);

 	   } catch (JiBXException e) {
 		   throw new RuntimeException("JiBXException: " + e.getMessage());
 	   } catch (IOException e) {
 		   throw new RuntimeException("IOException: " + e.getMessage());
 	   } catch (ClassNotFoundException e) {
 		   throw new RuntimeException("ClassNotFoundException: "
 				   + e.getMessage());
 	* * Read a schema definition into model from stream. * *
 	* @param is
 	*            schema input stream *
 	* @return schema element *
 	* @throws Exception
    protected Person readSchema(InputStream is) throws Exception {
 	   IUnmarshallingContext ictx = m_bindingFactory
 	   return (Person) ictx.unmarshalDocument(is, null);


How to bind the compiler into eclipse with ANT

More programming related pages

Workflow: Release process
Workflow: Bug tracking
Teambox (Redbooth) - Mantis connector
Design Patterns
Git & Github
Go / Golang
CVS backup script
Distribution process
Installation script
Java Server Faces
Perl tips
Perl links
Python / py2app
Shell scripts
Facebook / Opengraph
PHP developer notes
Redbooth API through php
Website optimization
jqTableKit demo
Javascript / html / css links