tags:

views:

120

answers:

3

I'm really stumped on this incredibly simple mapping. It looks just like one of the examples even. If I comment out the internal structure, it'll run the binding compiler successfully. If I put the internal structure back in, it fails. Note that the internal structure is just defining the XML. This is basically example5 of the JIBX tutorial examples.

<binding>
  <mapping name="RequestTransaction" class="TransactionRequest">
    <value name="version" set-method="setVersion" get-method="getVersion" style="attribute" />
    <structure name="transHeader">
      <value name="requestCount" set-method="setRequestCount" get-method="getRequestCount"/>
    </structure>
  </mapping>
 <binding>

Then I get the following error on the jibx compile:

Error: Error during validation: null; on mapping element at (line 2, col 97, in jibx-binding.xml)

I'm absolutely stumped and out of ideas. Google shows nothing useful.

A: 

Could you supply the java class that you're modelling? I also don't see example5 on the JiBX web site.

rcl
A: 

The <structure> is arguably the most important concept in JiBX binding because it allows you to map arbitrary XML to your Java classes without forcing you to create bloated and ugly layers of nested Java objects and classes to match the XML design.

In this case your binding declares that you have an XML element named <transHeader> that will not be present in your Java class.

With some slight fixes to your XML format, your binding works perfectly. I assume the fact that your binding has two <binding> open tags rather than and open and close <binding></binding> is a typo, because you said you got it to work without the structure. Also add <?xml version="1.0"?> at the top of your binding file. Those two XML mods allow the JiBX 1.2 binding compiler to work with the following Java class:

(Note: you didn't provide the Java class this binding is for so I had to reconstruct it from the info you put in the binding file. The obvious side effect of this is that I reconstructed a class that will work with this binding. But the simple fact is that a JiBX binding by design contains all the info you need to know about the class and the XML.)

public class TransactionRequest {
    private String version;
    private int requestCount;

    public void setVersion(String ver) {
     version = ver;
    }

    public String getVersion() {
     return version;
    }

    public void setRequestCount(int count) {
     requestCount = count;
    }

    public int getRequestCount() {
     return requestCount;
    }    
}

compile the class then run the binding compiler with:

>java -jar jibx-bind.jar jibx-binding.xml

To test it I used the following sample.xml:

(Note: you also didn't provide the XML you are trying to map so again I created a sample based on what you did provide)

<?xml version="1.0"?>
<RequestTransaction version="0.1">
    <transHeader>
        <requestCount>3</requestCount>
    </transHeader>
</RequestTransaction>

Running the test uses the following code:

    public static void main(String[] argz) {
     String fileName = "./sample.xml";
     IBindingFactory bfact = null;
     IUnmarshallingContext uctx = null;
     TransactionRequest sample = null;
     try {
      bfact = BindingDirectory.getFactory(TransactionRequest.class);
      uctx = bfact.createUnmarshallingContext();
      InputStream in = new FileInputStream(fileName);
      sample = (TransactionRequest)uctx.unmarshalDocument(in, null);
      System.out.println(sample.getRequestCount());
      System.out.println(sample.getVersion());
     }
     catch (Exception e) {
      e.printStackTrace();
     }
    }

And it runs successfully.

Mocky
A: 

It's been a while now, but I found it was related to inheritance. I needed to give mappings for everything in the inheritance tree, including interfaces as I recall.

I ended up creating a wrapper object, which I've found seems to be the easiest way to use JIBX in general. Trying to map a true domain class causes tendrils into every class that class touches and I have to unjar everything so JIBX can find the classes, including 3rd party libs.

Chris Kessel