Welcome to this Blog. I am Software Engineer and work for Zühlke Engineering AG in Bern. This is my private blog, in which I will post mainly about technical stuff like Software Engineering or IT related topics. The views expressed herein do not necessarily represent those of my employer.

 

 
 

 

We use JiBX in our current project and had the problem, that JiBX won’t trim whitespaces automatically. For example, a XML tag like <test> asdf  </test> get’s stored as “ asdf  “ instead of “asdf”.

To solve this problem, we created an user-defined formatter for Strings.

package ch.stefanjaeger.formatter; 
public class StringFormatter { 
  public static String trimString(String untrimmed) { 
    return untrimmed.trim(); 
  } 
}

Now just update the binding file of JiBX and JiBX will trim whitespaces:

<format 
deserializer="ch.stefanjaeger.formatter.StringFormatter.trimString" 
type="java.lang.String" />

 

 

 
 

 

Are you a real men (or women)? Because if you are one, Eclipse offers you the possiblity to “don’t click” ;-) Try to use the simple shortcut CTRL + 3. What happens? Eclipse opens the Quick access window:

image

 

Now, type what you want, for example “new xml file”, and Eclipse searches for the command, which consists of these words:

image

 

Also very interesting is the access to the Preferences window. Try to search for “classpath variables” or “build path”, you get direct access to these settings. Also interesting is “Generate Getters”. If you don’t know a specific shortcut, just use CTRL + 3 from now on!

 

 

 
 

 

This is the last hint this week. While working with JiBX, I had the situation, that there were some null values in the Java class.

JiBXTestApplication 
Exception in thread "main" org.jibx.runtime.JiBXException: null value for element 
"{http://stefanjaeger.ch/CustomerSchema}city" from object of type ch.stefanjaeger.jibx.Customer 
        at org.jibx.runtime.impl.MarshallingContext.element(MarshallingContext.java:713) 
        at ch.admin.bit.edec.common.declaration.Versandvorgang.JiBX_binding_marshal_1_0(Unknown Source) 
        at ch.admin.bit.edec.common.declaration.Deklaration.JiBX_binding_marshal_1_0(Unknown Source) 
        at ch.admin.bit.edec.common.declaration.JiBX_bindingDeklaration_access.marshal() 
        at ch.admin.bit.edec.common.declaration.Deklaration.marshal(Unknown Source) 
        at org.jibx.runtime.impl.MarshallingContext.marshalRoot(MarshallingContext.java:1041) 
        at org.jibx.runtime.impl.MarshallingContext.marshalDocument(MarshallingContext.java:1111) 
        at ch.admin.bit.test.jibx.JiBXTestApplication.main(Unknown Source)

Per default, JiBX doesn’t allow null values. If there is a null value, it throws an JiBXException while marshalling.

To allow null values, you have to define explicit the attribute usage="optional" on a value or structure element in the binding file. If JiBX finds a null pointer on a member, JiBX does not fill an empty value to the xml element,  JiBX will skip the element.

 

 

 
 

 

To define a constant value in an output mapping, just use the attribute constant:

<value constant="hello world" name="theAttribute" />

The XML attribute theAttribute get now always the value hello world.

 

 

 
 

 

I like to validate the binding XML file against a schema, to prevent typos and other mistakes. In the folder docs of the JiBX zip archive, there is the XML schema binding.xsd. Just use it like following:

<binding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation="binding.xsd">

 

 

 

« Newer PostsOlder Posts »